简体   繁体   English

Xamarin在Android / iOS上的常见Xamarin.Forms代码

[英]Xamarin common Xamarin.Forms code across Android/iOS

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). 我想在Visual Studio 2015中创建一个Xamarin Forms PCL,它代表Xamarin Android项目和Xamarin iOS项目的常见应用程序代码(但不是其他任何东西,即:Windows或Windows手机)。 I am currently using PCL profile 111 which is what is created by the provided Xamarin template. 我目前正在使用PCL配置文件111,它是由提供的Xamarin模板创建的。

All was fine until I came across support for System.IO.FileStream. 一切都很好,直到我遇到System.IO.FileStream的支持。 Based on this article (in the Saving and Loading file section): https://developer.xamarin.com/guides/xamarin-forms/working-with/files/ 基于这篇文章(在保存和加载文件部分中): 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. Android和iOS都可以直接使用System.IO中的大部分内容,这在我的Android和iOS项目中很明显,我可以使用FileStream来读取/写入文件。

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 我还在这里查看: http//blog.stephencleary.com/2012/05/framework-profiles-in-net.html

but the Portable Class Library profiles table doesn't mention Xamarin/Android/iOS. 但是Portable Class Library配置文件表没有提到Xamarin / Android / iOS。

Is there simply not a PCL profile any narrower than 111 which can provide what I want? 是否只有一个比111更窄的PCL配置文件可以提供我想要的东西? Is my only option to use a DependencyService setup? 我唯一的选择是使用DependencyService设置吗? 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. 编辑 :虽然我提到FileStream作为我正在尝试做的一个例子,我想解决维护Android / iOS的任何常见代码的问题(并且只有那些平台,而不是Windows / Windows Phone / Silverlight / ASP。对于Android和iOS使用通用代码支持功能的任何情况,NET等),但其中一个平台没有。

You can use PCLStorage Library for cross platform IO: 您可以将PCLStorage Library用于跨平台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. 尝试编辑Xamarin Studio中的设置。 In Xamarin Studio the different Xamarin targets listed as target options for a PCL. 在Xamarin Studio中,不同的Xamarin目标被列为PCL的目标选项。 For Profile 111 you will see that the Xamarin platforms are being targeted. 对于Profile 111,您将看到Xamarin平台正在成为目标。 A shared project might be your best option in this instance. 在这种情况下,共享项目可能是您的最佳选择。

I think you should try File System Plugin (Xamarin Component) 我想你应该尝试文件系统插件 (Xamarin组件)

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. Xamarin和Windows的文件系统插件为.NET,Windows Phone,Windows Store,Xamarin.iOS,Xamarin.Android和Silverlight提供了一致的,可移植的本地文件IO API集。 This makes it easier to create cross-platform .NET libraries and apps. 这样可以更轻松地创建跨平台的.NET库和应用程序。

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 Thia将允许您在常见的PCL项目中编写所有文件操作

Is there simply not a PCL profile any narrower than 111 which can provide what I want? 是否只有一个比111更窄的PCL配置文件可以提供我想要的东西? Is my only option to use a DependencyService setup? 我唯一的选择是使用DependencyService设置吗? Maybe a Shared Code project would be cleaner? 也许共享代码项目会更清洁?

Profile 111 is the "closest" you are going to get. 配置文件111是您将获得的“最接近”。

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. 对于像这样的情况,您只需要在解决方案中使用Xamarin.Android和Xamarin.iOS`支持,使用共享项目是最快速,最干净的方式。 Shared PCL Libraries are great, BUT, if you do not need all that cross-platform support (and thus restrictions) go with shared files... 共享PCL库很棒,但是,如果您不需要所有跨平台支持(因此限制),请使用共享文件...

Later on, if need, you can always migrate that code to a PCL-based library and/or a platform dependent one. 稍后,如果需要,您始终可以将该代码迁移到基于PCL的库和/或依赖于平台的库。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM