【问题标题】:Caching variables in isolated storage, portable class library way以隔离存储、可移植类库方式缓存变量
【发布时间】:2013-10-03 06:33:55
【问题描述】:

我正在尝试弄清楚如何读取和写入存储在应用程序隔离存储中的参数。

现在我正在构建一个 windows phone 应用程序,但由于我想要一个 win8 应用程序,所以我想我可以在可移植类库项目中完成它,并发现这很棒PCLStorage

我的 Cache 类看起来像这样用于存储参数:

    public async static Task<string> GetParam(string name)
    {
        IFolder rootfolder = FileSystem.Current.LocalStorage;
        IFolder folder = await rootfolder.GetFolderAsync("isostore");
        IFile file = await folder.GetFileAsync(name);

        return await file.ReadAllTextAsync();
    }

    public async static void SaveParam(string name, string param)
    {
        IFolder rootfolder = FileSystem.Current.LocalStorage;
        IFolder folder = await rootfolder.CreateFolderAsync("isostore", CreationCollisionOption.OpenIfExists);
        IFile file = await folder.CreateFileAsync(name, CreationCollisionOption.ReplaceExisting);

        await file.WriteAllTextAsync(param);
    }

写部分没问题,如果有的话,它会覆盖。它的阅读部分是问题所在。 IFile 和 IFolder 没有任何 .Exists 函数 (???) 那么如果我在保存之前调用 Get 会返回什么?

【问题讨论】:

    标签: c# portable-class-library


    【解决方案1】:

    我认为在您的情况下,在 GetParam 方法中,您应该使用 CreationCollisionOption.OpenIfExists 参数调用 CreateFolderAsync 和 CreateFileAsync。那么您不必担心事先单独创建它们或捕获它们存在/不存在的异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-23
      • 1970-01-01
      • 2011-04-29
      相关资源
      最近更新 更多