在WinRT中加载文件后出现异常

时间:2014-01-20 23:19:23

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

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

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;
}

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

1 个答案:

答案 0 :(得分:4)

根据名称,我怀疑SaveBaseFileAsync()执行一些异步操作。如果确实如此,则需要等待它,即将其称为

await SaveBaseFileAsync(filelink, folder, basefilename);