2013-05-16 34 views
1

我已阅读了大约一千个类似的帖子,并且遵循了一般性建议,但仍然遇到了这个问题。这是我的场景:IsolatedStorageException在调用CreateFile时不允许执行操作

我正在研究一个Windows Phone 8应用程序,当用户保存时,它将所有数据序列化为XML,然后使用CreateFile来存储它。我面临的问题是,如果用户连续点击保存多次,会抛出IsolatedStorageException:Operation Not Permitted(我猜测序列化需要足够长的时间,以至于当我尝试访问它时,该文件仍在使用中第二次)。第二次点击保存时,有没有办法让我放弃上一个操作,释放孤立的存储文件,并启动新的保存?还是有更好的解决方案?

下面的代码为我保存方法(发生在isoStore.CreateFile(文件名)线除外):

 using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 

      using (IsolatedStorageFileStream stream = isoStore.CreateFile(filename)) 
      { 
       XmlSerializer xml = new XmlSerializer(GetType()); 
       xml.Serialize(stream, this); 
      } 
     } 

任何帮助将是惊人的,因为我已经在这里停留几个星期。

谢谢, 本:

回答

1

为什么不能禁用“保存”按钮,一旦完成系列化点击时再次启用它?

2

你可以去这样的事情。

private async Task Save(string fileName) 
{ 
    Button.IsEnabled = false; 

    await Task.Run(() => 
     { 
      using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) 
      { 

       using (IsolatedStorageFileStream stream = isoStore.CreateFile(filename)) 
       { 
        XmlSerializer xml = new XmlSerializer(GetType()); 
        xml.Serialize(stream, this); 
       } 
      } 
     }); 

    Button.IsEnabled = true; 
}