可移植类库不支持System.IO,为什么呢? [英] Portable Class Library does not support System.IO, Why?

查看:259
本文介绍了可移植类库不支持System.IO,为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个便携式类库以用在我的 Monodroid项目。 但问题是,我需要的 System.IO 库,但不幸的是我不能添加它。

我甚至尝试添加它通过添加引用的选项,但它是徒劳的。

为什么发生这种情况? 我该怎样做呢?

解决方案

您无法使用 System.IO ,因为它不是一个便携式类库。 System.IO 进行调用特定于它运行(视窗)操作系统,而便携式类库彪是跨平台的。

要解决你在找什么都可以找到<一href="http://blogs.msdn.com/b/dsplaisted/archive/2012/08/27/how-to-make-portable-class-libraries-work-for-you.aspx">here:

  

你应该做的,当你试图写一个可移植库,但   你需要的是不支持某些功能?你不能打电话   API直接,你不能引用,做一个图书馆,因为   便携式库不能引用不可移植的库。该   解决方案是在您的便携式库中创建一个抽象的   提供您需要的功能,并实现了抽象   每个平台的移植库的目标。例如,如果   需要保存和载入的文本文件,你可以用一个接口一样   这样的:

 公共接口IFileStorage
{
    任务SaveFileAsync(字符串的文件名,字符串内容);
    任务&LT;字符串&GT; LoadFileAsync(字符串文件名);
}
 

  

这是一个好主意,只包括您需要的功能   抽象。在本实施例中,接口不抽象一般   文件系统的概念,如流,文件夹或文件枚举。   这使得更抽象便携和更容易实现。该   方法返回的任务,这样的实现Windows应用商店的应用程序   可以调用WinRT的文件IO的API,这是异步。

     

创建了一个抽象允许便携式库调入   非便携式code,而这种模式是适用的,几乎任何时候你   需要从便携式库访问非便携式功能。的   当然,你需要某种方式的便携式code获得引用   实现抽象的。你怎么能依赖   无论你是编写跨平台的应用程序或通用   可重复使用的库。

I created a portable class library to be used in my Monodroid project. But the problem is that I need System.IO library but unfortunately I couldn't add it.

I even tried to add it by Add Reference option but it was in vain.

Why this happened ? How shall I do this ?

解决方案

You can't use System.IO because it isn't a portable class library. System.IO makes calls which are specific to the OS it runs on (Windows), while the portable class library is ment to be cross-platform.

The solution to what you're looking for can be found here:

What should you do when you’re trying to write a portable library but you need some functionality that isn’t supported? You can’t call the API directly, and you can’t reference a library that does, because portable libraries can’t reference non-portable libraries. The solution is to create an abstraction in your portable library that provides the functionality you need, and to implement that abstraction for each platform your portable library targets. For example, if you need to save and load text files, you might use an interface like this:

public interface IFileStorage 
{
    Task SaveFileAsync(string filename, string contents);
    Task<String> LoadFileAsync(string filename); 
} 

It’s a good idea to include only the functionality you need in the abstraction. In this example, the interface doesn’t abstract general file system concepts such as streams, folders, or enumerating files. This makes the abstraction more portable and easier to implement. The methods return Tasks so that the implementation for Windows Store apps can call the WinRT file IO APIs, which are async.

Creating an abstraction allows portable libraries to call into non-portable code, and this pattern is applicable almost any time you need to access non-portable functionality from a portable library. Of course, you need some way for the portable code to get a reference to an implementation of the abstraction. How you do that can depend on whether you are writing a cross platform app or a general purpose reusable library.

这篇关于可移植类库不支持System.IO,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆