I have some issues on a Xamarin project to access to a file from the core lib.
My file is schema.json it's located in Assets folder of the Android project and in the Resources folder of the Touch project but neither Application.Context.Assets.Open("schema.json")
or System.IO.File.ReadAllText("schema.json")
works.
Do you know how do access to a specific file/folder from the core lib ?
PS : I am using MVVM Cross if it can help.
Assuming you're using Xamarin.Forms and trying to do this in a cross-platform manner, you can use the examples in this guide, like so:
var assembly = typeof(LoadResourceText).GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream("WorkingWithFiles.PCLTextResource.txt");
string text = "";
using (var reader = new System.IO.StreamReader (stream)) {
text = reader.ReadToEnd ();
}
To do this, you'll need to add your file to your Forms PCL project, with a Build Action of EmbeddedResource
.
For loading platform specific strings, near the bottom you can see the standard practice of using IoC to get an implementation that can access the platform-bundled file content.