Xamarin Essentials FileSystem 可以保存异步吗 [英] Xamarin Essentials FileSystem can you save async

查看:47
本文介绍了Xamarin Essentials FileSystem 可以保存异步吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

了解 Xamarin 基本要素并想知道 FileSystem 的目的是什么.

Looking into Xamarin essentials and wondering what the purpose of FileSystem is.

我正在寻找一种可以立即为我异步加载和保存文件的东西.

I was looking for something that would load and save a file for me async out of the box.

它在我看来,可能是我不理解它给您的唯一功能是保存和加载文件的位置,并且没有实际为您保存它的功能,例如旧 PCLStorage"

It looks to me and may be I am not understanding the usage that the only thing that it gives you is a location where to save and load the file and there is no functionality to actually save it for you eg "Old PCLStorage"

这是正确的吗?

推荐答案

所有 Xamarin Essentials 文件系统 Helpers 正在做的是提供对应用内两个不同目录的访问,CacheDirectoryAppDataDirectory 并访问应用程序内的只读预捆绑文件,因此您不必为这些位置编写基于平台的代码并执行您自己的 DI(或使用 Forms 的 DI)来访问它们...

All Xamarin Essentials File System Helpers are doing is providing access to two different directories within your app, CacheDirectory and AppDataDirectory and access to the read-only pre-bundled files within application so you do not have to write platform-based code for these locations and do your own DI (or use Forms' DI) to access them...

一旦您有了基于 string 的目录位置,您就可以使用普通的 .Net(Std) 文件和目录 I/O 例程.创建/删除子目录,使用 .Net(Std) 框架或第三方框架/包中的异步(或非)函数创建/读取/写入/删除文件.你的选择...

Once you have the string-based directory location, then you use the normal .Net(Std) file and directory I/O routines. Create/delete subdirectories, create/read/write/delete a file using the async (or not) functions from the .Net(Std) framework or a third-party framework/package. Your choice...

缓存目录是一个读/写位置,它实现了对原生"平台缓存位置的访问.在 Android 上这是缓存

The cache directory is a read/write location and it implements access to the "native" platform cache location. On Android this is the Cache

var cacheDir = FileSystem.CacheDirectory;

应用数据目录

应用数据目录是应存储常规文件的默认"位置.

AppDataDirectory

The app data directory is the "default" location for where regular files should be stored.

如文档所述:

任何不是用户数据文件的文件

any files that are not user data file

仅供参考:这是不是用于Sqlite数据库的地方,如果您正在实施/遵守平台相关规范......为什么Essentials不包括数据库位置我不知道当像 iOS 和 Android 这样的平台有 API 和正式记录的位置时......;-/

FYI: This is not the place to be used for Sqlite databases and such if you are implementing/complying with platform dependent norms... Why Essentials did not include a database location is unknown to me when platforms like iOS and Android have APIs for them and formally documented locations... ;-/

OpenAppPackageFileAsync 提供对应用程序中只读捆绑文件的访问(通过 Stream)(AndroidAssetsBundleResource等.)

OpenAppPackageFileAsync provides access (via a Stream) to read-only bundled files in your application (AndroidAssets, BundleResource, etc..)

using (var stream = await FileSystem.OpenAppPackageFileAsync("sushiLogo.png"))
{
    using (var reader = new StreamReader(stream))
    {
        var fileContents = await reader.ReadToEndAsync();
    }
}

这篇关于Xamarin Essentials FileSystem 可以保存异步吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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