2014-01-20 28 views
1

我有一些問題。這裏是我的代碼:例外,在WinRT加載文件後

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

有沒有人知道如何解決這個問題?文件下載工作正常,下載後文件創建在一個文件夾中。

+0

你爲什麼不只是檢查null? – i3arnon

+0

這很簡單,因爲我應用程序首次啓動時需要此文件。 問題是爲什麼當應用第一次啓動時它爲空。 – ivamax9

+0

可能是錯誤的「CheckExistingBase()」,使它返回「true」,即使該文件不存在?只是一個猜測 – har07

回答

4

基於名稱,我懷疑SaveBaseFileAsync()會執行一些異步操作。如果這是真的,你需要等待它的,即把它作爲

await SaveBaseFileAsync(filelink, folder, basefilename);

+0

非常感謝安德烈,它幫助! – ivamax9