Skip to main content
 首页 » 编程设计

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

2024年02月13日12fff_TT

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

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(文件链接、文件夹、基本文件名);