c# - 可移植类库不支持System.IO,为什么?

标签 c# .net xamarin.android portable-class-library

我创建了一个可移植类库,用于我的Monodroid 项目。 但问题是我需要 System.IO 库,但不幸的是我无法添加它。

我什至尝试通过“添加引用”选项添加它,但没有成功。

为什么会这样? 我该怎么做?

最佳答案

您不能使用 System.IO,因为它不是可移植类库。 System.IO 进行特定于其运行的操作系统 (Windows) 的调用,而可移植类库是跨平台的。

可以找到您正在寻找的解决方案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.

关于c# - 可移植类库不支持System.IO,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25460581/

相关文章:

c# - 调用简单服务失败并显示明显错误的错误消息

c# - 通知所有组件 Blazor 中的身份验证已更改

c# - 将结构从 C# 编码到 C++ DLL

c# - 用 <p> 段落和 <br/> 标签替换换行符

安装 Hyper V 后,Android SDK 的 Intel HAXM 被禁用

c# - DataGridView 和 ComboBox 问题

c# - 从 MySQL 表接收 DownloaLink

c# - listViewItem.BackColor 不工作

Xamarin Forms 已部署 Textview

c# - 如何在 Xamarin Forms 中的 "re-adding"TabbedPage 子项时禁用 BottomNavigationView 切换模式?