Xamarin - Shared Class Library Accessing System.IO.File - c#

I try to create a really simple app containing
Windows Version
Mac Version
Portable Class Library
I would like to put the code (as much as possible) into the Portable Class Library.
Now i simply want to check if a file exists using System.IO.File.Exists() but the File object is not available. I tryed to target different plattforms but i cant get it to work.

What #hvaughan3 said is correct in that the System.IO.File is not available from your PCL and it needs to be implemented within each platform and injected into your PCL. That said the good news is that like many of these common requirements there already is a plugin to do that so you don't need to do it yourself.
Have a look at the following blog post that describes how to use the PCLStorage plugin within your PCL project.

System.IO.File is not available in the PCL code. You will need to implement in platform specific code. You will also need to verify that it exists for each platform. I know it does for Android and iOS, have never tried using it on Mac and Windows. Although from Xamarin's docs it looks like at least Mac does have System.IO.File.
Once you have methods in each platform project, you can reference the platform specific method from your PCL code using Xamarin's Dependency Service.

Related

How to find out which dependency is missing after DllNotFoundException?

I want to write a small .Net MAUI application which makes use of a C# SDK NuGet Package (implementing Hyperledger Anoncreds).
When running the App on Android (emulator or device) most of the SDK method calls work, but certain calls still throw a DllNotFoundException without any indication of which Dll is actually missing.
After I did some reading on the SDK, I was able to add some .so dependencies to the MAUI project, but apparently there are still some missing.
I already tried reading through the SDK source code and some of the Rust code it wraps to get hunch of what could possibly be missing. But even wading through the cargo.toml files only show me the crates and not any actually libraries.
Android tools like "APK Analyzer" provide a lot of usefull information, but no dependency overview and the "Dependency Walker" tool only analyzes Windows-built binaries which differs from the actual Android environment.
How do I find the missing dependency indicated by the Exception? Is there a tool like "Dependency Walker" for Android APKs?
Edit: Fixed some typos and added more Info.
After a quick google search for an equivalent of ldd on Android I found this:
ndk-depends which should be shipped with Android NDK. I would try and use that on the .so files that you use and include with your project.
EDIT: Here is a link to the manual: ndk-depends
Hope it helps!

Can the Xamarin Dependency service work if the caller is not using Xamarin.Forms?

Simple question, can the Xamarin.Forms Dependency Service still Get the platform of the caller if the caller is not Xamarin.Forms based or even Xamarin at all?
Now for the setup to the question. I have two app projects, Android and Windows UWP. For various reasons, I don't need iOS and I cannot use Xamarin.Forms for the two. So, I have a native UWP application and Xamarin.Android. These two can still share a lot of code with PCL or better, .NetStandard.
If I am creating libraries, as I am loosely following this post Dependency Service , and the caller of a library like this is Xamarin.Android or UWP (ie not Xamarin Forms) will the Dependency Service in the library still know the platform of the caller? The bottom line is, in the library, I need to know what platform is consuming or calling the library so I can do platform specific code within the library.
As a follow up to this question, a lot of the examples I have seen with Dependency Service use a PCL. Since everybody seems to be moving away from PCL and towards .NetStandard libraries, can this same thing be achieved if the library is .NetStandard?
Thanks!
If you are not using the Xamarin Forms library, then you will not have access to the DependencyService class.
You can use one of the many other DI libraries such as Autofac which is compatible with .NET Standard projects.

Xamarin System.Data.Linq inside PCL

I've got a PCL which includes System.Data.Linq(.Mappings) via the dll itself. This works with iOS, Windows and MacOS, but it does not with Android.
Temp: I will post the error message as soon as I am back at work, but I remember it was something like "Could not load file or assembly System.Data.Linq". It was not found
I know this namespace is not supported inside a PCL, but it is strange that it works (and yes, I made multiple projects with this) in every project type except Xamarin:Android.
Is there anything I could try like implementing the code myself, embed assemblies in native code, or something else?
Thanks for your help
Is there anything I could try like implementing the code myself, embed assemblies in native code, or something else?
I think no. As you already known, System.Data.Linq is unavailable in PCLs, and there is no lambda expressions in Android (or Java) yet. Also we need to respect the design of PCL, it is used to write and build managed assemblies that work on more than one .NET Framework platform. So if the method/dll or something else is not supported in the projects which uses this PCL, then this method/dll should not be placed in PCL.
Also, Xamarin.Forms is designed as a cross-platform UI toolkit that allows developers to easily create native user interface layouts that can be shared across Android, iOS, and Windows Phone. It is mainly target sharing UI. Of course you can also specify the platform when calling method/dll in its PCL, for example like this:
#if __MOBILE__
// Xamarin iOS or Android-specific code
#endif
For more info about specifying platform, you can refer to Dealing with Multiple Platforms.

Missing targets in VS 2013

I'm working with compiling the google apps API and I haven't seen it where I'm missing.
.net and windows 8/8.1 are both listed but nothing else. I'm running VS 2013 on a Windows 7 computer.
Because of this, when I go into references to the project, I'm missing all of the original microsoft references (even if I go to add them, there's nothing to select from).
For example, the error I'm getting is: Warning 2 The primary reference "System.IO", which is a framework assembly, could not be resolved in the currently targeted framework. ".NETPortable,Version=v4.0,Profile=Profile5". To resolve this problem, either remove the reference "System.IO" or retarget your application to a framework version which contains "System.IO". Google.Apis.Admin.Directory.directory_v1
I'm not sure why that and all the other microsoft API's are not showing up when I go to select them (or not allowing me to compile). I assume it's because of the .netportable and the fact I'm not on a Windows 8.x computer.
Anyone know how to change the target? I don't see anyway of doing that.
What you have there is a Portable Class Library. It's lacking many namespaces and functions that are native to Windows only; basically, a PCL is reduced to the minimum feature set of .NET that can run on all target platforms.
For many missing namespaces there are replacement PCLs; if none is available, you can define interfaces in the PCL (and implement them in platform-specific code) that serve as a "bridge" between the PCL and e.g. the System.IO namespace on Win32.
Simple example: You define IFileIO with WriteFile(...) and ReadFile(...) in the PCL; then every project that uses the PCL (ex.: WindowsIO Windows) has a class that implements IFileIO and executes the System.IO calls within the respective functions.
Then you combine the Windows-specific implementation with the PCL interface; imagine you have a ImageConverter class that reads, modifies and writes images. Its constructor accepts an IFileIO, where you can pass a WindowsIO instance (or Win8IO, or AndroidIO, or ...). The ImageConverter only uses the interface and doesn't care about the implementation.

Sharing C# code between Windows and Silverlight class libraries

We wrote a small Windows class library that implements extension methods for some standard types (strings initially). I placed this in a library so that any of our projects would be able to make use of it by simply referencing it and adding using XXX.Extensions.
A problem came up when we wanted to use some of these methods in Silverlight. Although all the code was compatible, a Windows library can't be referenced in Silverlight so we created a Silverlight library that had links to the same class files and put compiler directives into the classes to allow different using declarations and namespaces. This worked fine until today when I added a new class to the Windows extensions library and realised that I would have to remember to link the class into the Silverlight library too.
This isn't ideal and I wondered if anyone might have ideas for a better way of sharing extension methods and other helper code between Windows and Silverlight projects.
You cannot set a reference from a Silverlight assembly to a regular .NET assembly but you can do so the other way round.
So create a shared Silverlight assembly and add your code to that assembly. Now you can set a reference fro both your regular .NET and you other Silverlight assembly to the shared Silverlight assembly.
The restriction is that you can only put code in there that would work on both the .NET and Silverlight CLR but that is no different from sharing code.
Since this question has been answered, there is a new solution from Microsoft, Portable Class Libraries. Here is the blog post where they announced it.
I'm just about to start playing round with them for sharing code between silverlight and some server side code I'm writing, so can't add too much past the links at the moment.
Silverlight runtime is different from the normal .NET runtime. So you need to do tricks at the project level to share code between multiple platforms.
Here's how I've done this for Autofac IoC container.
With this approach you do not have to create different projects for each platform being targeted.
PS: there is also a Project Linker tool from the Composite WPF that allows to link Silverlight and WPF projects (creates multiple projects). But it does look messy.
there is a similar problem with XNA projects. Since you can target several different platforms, you're required to have different projects. The reason for this is because the base class libraries that the project references are platform specific, so you can't have just one project.
If you're curious, you can get a bit of insight from this blog:
To recompile the source for another
platform, you need another project.
The reason for this is because the
projects need to reference different
assemblies for both the XNA Framework
and the underlying .NET Framework
(Xbox 360 and Zune use the .NET
Compact Framework), and C# projects
don't provide support for referencing
different assemblies for different
platforms.
Try this http://buildassilverlight.codeplex.com/
I had some dependency issues when referencing to a Silveright class library in .Net.
An alternative way when using Visual Studio 2010 and WCF RIA 1.0:
Create a normal .Net library assembly.
Create a Silverlight class library. In the configuration of the assembly specifiy the first .NET library as the "WCF RIA Service link"
Put your code in the .NET library as "ClassName.shared.cs" files.
WCF RIA will handle copying the file to the Silverlight assembly.

Categories

Resources