I'm looking to create a Xamarin Forms PCL in Visual Studio 2015 that represents common application code for a Xamarin Android project and Xamarin iOS project (but NOT anything else, ie: Windows or Windows phone). I am currently using PCL profile 111 which is what is created by the provided Xamarin template.
All was fine until I came across support for System.IO.FileStream. Based on this article (in the Saving and Loading file section): https://developer.xamarin.com/guides/xamarin-forms/working-with/files/
Both Android and iOS can use much of System.IO right out of the box, and this is evident in my Android and iOS projects where I can use FileStream identically to read/write files.
Since the code is identical, it would make a lot more sense to maintain it in a single place, but I can't seem to track down a profile (or visual studio template) that will do this, any ideas?
I have additionally looked through here: http://blog.stephencleary.com/2012/05/framework-profiles-in-net.html
but the Portable Class Library profiles table doesn't mention Xamarin/Android/iOS.
Is there simply not a PCL profile any narrower than 111 which can provide what I want? Is my only option to use a DependencyService setup? Maybe a Shared Code project would be cleaner?
EDIT : Although I mention FileStream as an example of what I'm trying to do, I would like to solve the problem of maintaining any common code for Android/iOS (and only those platforms, not Windows/Windows Phone/Silverlight/ASP.NET etc) for any case where Android and iOS support a feature using common code, but one of those other platforms does not.
You can use PCLStorage Library for cross platform IO:
public async Task PCLStorageSample()
{
IFolder rootFolder = FileSystem.Current.LocalStorage;
IFolder folder = await rootFolder.CreateFolderAsync("MySubFolder",
CreationCollisionOption.OpenIfExists);
IFile file = await folder.CreateFileAsync("answer.txt",
CreationCollisionOption.ReplaceExisting);
await file.WriteAllTextAsync("42");
}
Try editing the settings in Xamarin Studio. In Xamarin Studio the different Xamarin targets listed as target options for a PCL. For Profile 111 you will see that the Xamarin platforms are being targeted. A shared project might be your best option in this instance.
I think you should try File System Plugin (Xamarin Component)
The File System Plugin for Xamarin and Windows provides a consistent, portable set of local file IO APIs for .NET, Windows Phone, Windows Store, Xamarin.iOS, Xamarin.Android, and Silverlight. This makes it easier to create cross-platform .NET libraries and apps.
Here is a sample
public async Task CreateRealFileAsync()
{ // get hold of the file system IFolder
rootFolder = FileSystem.Current.LocalStorage; // create a folder, if one does not exist already
IFolder folder = await rootFolder.CreateFolderAsync("MySubFolder", CreationCollisionOption.OpenIfExists); // create a file, overwriting any existing file
IFile file = await folder.CreateFileAsync("MyFile.txt", CreationCollisionOption.ReplaceExisting); // populate the file with some text
await file.WriteAllTextAsync("Sample Text...");
}
Thia will let you write All file operations in a common PCL project
Is there simply not a PCL profile any narrower than 111 which can provide what I want? Is my only option to use a DependencyService setup? Maybe a Shared Code project would be cleaner?
Profile 111 is the "closest" you are going to get.
Personally for cases cases like this where you only need Xamarin.Android
and Xamarin.iOS` support in your solution, using a Shared Project is the fastest and cleanest way. Shared PCL Libraries are great, BUT, if you do not need all that cross-platform support (and thus restrictions) go with shared files...
Later on, if need, you can always migrate that code to a PCL-based library and/or a platform dependent one.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.