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软件包。