【问题标题】:Exception, after loading file in WinRT异常,在 WinRT 中加载文件后
【发布时间】:2014-02-10 08:08:37
【问题描述】:

我遇到了一些问题。这是我的代码:

private async void SetCollectionForGame()
{
    maincollection = new Dictionary<string, string>();

    bool statebase = await CheckExistingBase();

    if (statebase)
    {
        //If file exists...
        basefile = await folder.GetFileAsync(basefilename);
    }
    else
    {
        //If file does not exist...
        SaveBaseFileAsync(filelink, folder, basefilename);
        basefile = await folder.GetFileAsync(basefilename);
    }
    string content = String.Empty;
    content = await FileIO.ReadTextAsync(basefile, Windows.Storage.Streams.UnicodeEncoding.Utf8);
    //When the app first starts, I get an exception on the next line,
    //because the variable "content" is null.
    maincollection = JsonConvert.DeserializeObject<CollectionModel>(content).collection;
}

有人知道如何解决这个问题吗?文件下载工作正常,下载后文件在文件夹中创建。

【问题讨论】:

  • 你为什么不只检查空值?
  • 很简单,因为我在应用程序启动时需要这个文件。问题是为什么它在应用首次启动时为空。
  • CheckExistingBase() 可能有问题,即使文件不存在,它也会返回true?只是猜测

标签: c# windows-runtime async-await windows-8.1 c#-5.0


【解决方案1】:

根据名称,我怀疑 SaveBaseFileAsync() 执行了一些异步操作。如果是真的,你需要等待它,即将它称为

await SaveBaseFileAsync(filelink, folder, basefilename);

【讨论】:

    猜你喜欢
    • 2012-11-05
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多