0

我在此頁面上教程http://channel9.msdn.com/Series/Windows-Phone-8-1-Development-for-Absolute-Beginners/Part-22-Storing-and-Retrieving-Serialized-Data,因爲我想讓我的應用程序將數據存儲在JSON文件中,然後再讀取它。關於教程的2件事:無法讀取JSON文件 - FileNotFoundException

我按寫按鈕 - 工作正常,然後按讀按鈕,它也可以,但是,我關閉win手機8.1模擬器,再次打開它,按讀取按鈕,我得到了An exception of type 'System.IO.FileNotFoundException'exception!爲什麼是的,我應該從以前運行的應用程序已經在磁盤上的文件?或者當我關閉仿真器時它會被擦除嗎?此外,我查找磁盤上的指定文件,無法找到它!任何幫助?

private const string JSONFILENAME = "data.json"; 

private async Task readJsonAsync() 
{ 

    string content = String.Empty; 

    var myStream = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(JSONFILENAME); 
    using (StreamReader reader = new StreamReader(myStream)) 
    { 
     content = await reader.ReadToEndAsync(); 
    } 

    resultTextBlock.Text = content; 
} 


private async Task writeJsonAsync() 
{ // Notice that the write is ALMOST identical ... except for the serializer. 

    var myCars = buildObjectGraph(); 

    var serializer = new DataContractJsonSerializer(typeof(List<Car>)); 
    using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(
        JSONFILENAME, 
        CreationCollisionOption.ReplaceExisting)) 
    { 
     serializer.WriteObject(stream, myCars); 
    } 

    resultTextBlock.Text = "Write succeeded"; 
} 
+2

請格式化您的代碼。 – 2014-09-29 18:07:36

+0

對不起,用過報價! – Dodi 2014-09-29 18:12:54

回答

0

當您關閉仿真器不保留其狀態,這意味着你測試的應用程序不存在,當你重新啓動仿真。因此,當您再次打開模擬器時,VS將進行新的安裝。

當您重建項目時會發生同樣的情況。然後,VS將從模擬器中刪除應用程序,並重新從頭開始重新安裝。這反過來也會導致丟失你的JSON文件。

當你想確保你的數據被保存,那麼不要關閉模擬器,只使用VS中的Build(VS決定不時重建你的應用)。

爲了更正確地測試您的應用程序,我建議您看看Windows Phone電動工具(http://wptools.codeplex.com/)。您可以明確選擇安裝或更新給定的XAP軟件包。