使用Xamarin.Forms中的PCLStorage读取文本文件

Élo*_*tit 6 xamarin.forms

有没有办法用PCLStorage模块读取PCL项目中的嵌入式JSON文件?我到处寻找,但找不到与此事有关的样本.

编辑:PCLStorage链接:https://github.com/dsplaisted/pclstorage

Igo*_*gor 4

你需要这样的东西:

public static async Task<string> ReadFileContent(string fileName, IFolder rootFolder)
{
  ExistenceCheckResult exist = await rootFolder.CheckExistsAsync(fileName);

  string text = null;
  if (exist == ExistenceCheckResult.FileExists)
  {
    IFile file = await rootFolder.GetFileAsync(fileName);
    text = await file.ReadAllTextAsync();
  }

  return text;
}
Run Code Online (Sandbox Code Playgroud)

使用:

IFolder rootFolder = FileSystem.Current.LocalStorage;
IFolder myCoolFolder = await rootFolder.CreateFolderAsync("MyCoolForler", CreationCollisionOption.OpenIfExists);
string fileContent = await this.ReadFileContent("MyCoolFile.txt", myCoolFolder);
Run Code Online (Sandbox Code Playgroud)

  • IFolder rootFolder = FileSystem.Current.LocalStorage; 您将拥有根文件夹,具体取决于平台 (2认同)