c# - 异常,在 WinRT 中加载文件后

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

有人知道怎么解决吗?文件下载工作正常,下载后文件在文件夹中创建。

最佳答案

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

await SaveBaseFileAsync(文件链接、文件夹、基本文件名);

关于c# - 异常,在 WinRT 中加载文件后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21246139/

相关文章:

c# - SortedSet 和 SortedList 由于不同的枚举而失败

c# - makepri.exe 是如何集成到 MSBuild 中的?

c# - ASMX Web 服务问题在单独的线程中与 Windows 服务通信

c# - 如何在 ActiveRecord 中将泛型类作为 ColumnType 传递?

c# - 无法从 xml 文件中获取数据

c++ - WinRT C++ (Win10) 从 SoftwareBitmap/BitmapBuffer 访问字节

c# - 动态绑定(bind)到 DataTemplate

node.js - Nodejs Promise async/await 无法正常工作

c# - 使用 async await 处理 Web 请求的超时

asynchronous - Rust中异步/等待的目的是什么?