2013-02-15 159 views
0

我将数据保存在隔离存储器中,但是当我重新启动手机时,我无法从此处读取此数据。隔离存储空置。为什么?WP7将数据保存在隔离存储器中

如果我不关闭手机中的所有工作确定

这是我的代码:

using (Stream file = IsolatedStorageHelper.OpenFile(USER_ACCOUNT_FILE, fileMode.Create)) 
     { 
      if (null != file) 
      { 
       try 
       { 
        XDocument xml = new XDocument(); 
        XElement root = new XElement("userAccount"); 

        root.Add(new XAttribute("FirstName", this._firstName)); 
        root.Add(new XAttribute("LastName", this._lastName)); 
        root.Add(new XAttribute("ID", this._id)); 
        root.Add(new XAttribute("Sex", this._sex)); 

        xml.Add(root); 

        // save xml data 
        xml.Save(file); 
       } 
       catch 
       { 
       } 
      } 
     } 

功能什么Issolated存储

static public IsolatedStorageFileStream OpenFile(string aFilename, FileMode mode) 
      { 
       IsolatedStorageFileStream res = null; 

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

        try 
        { 
         res = new IsolatedStorageFileStream(aFilename, mode, FileAccess.ReadWrite, isoStore); 
        } 
        catch (Exception exc) 
        { 
         if ((null != (exc as IsolatedStorageException)) && 
          (FileMode.Open != mode) && 
          (true == createPathOnIsolatedStorage(isoStore,aFilename))) 
         { 
          try 
          { 
           res = new IsolatedStorageFileStream(aFilename, mode, isoStore); 
          } 
          catch 
          { 
          } 
         } 
        } 
       } 

       return res; 
      } 
+1

你使用实际的设备还是你正在谈论模拟器? – 2013-02-15 15:02:23

+0

当您的应用程序启动时会发生什么? OpenFile()被调用了吗?您应该用于OpenFile的FileMode应该是FileMode.OpenOrCreate,而不是FileMode.Open。 FileMode.Open将始终重新创建该文件并覆盖您以前拥有的任何数据。 – Gambit 2013-02-15 21:44:52

+0

我在模拟器上测试过。我已经在手机上运行应用程序,一切都变得有效。谢谢 – Nadezhda 2013-02-18 06:57:39

回答

2

创建文件。如果你是在谈论在模拟器上运行这个,这是正常行为。模拟器默认情况下不保存独立存储。

物理设备将始终将数据保存在存储器中,除非它被明确重置,应用程序被卸载或用户通过应用程序提供的某种方式删除了内容。

+0

谢谢。你是对的,我在模拟器上测试了这个应用程序,当我在手机上测试它时,所有工作都开始良好 – Nadezhda 2013-02-18 06:50:08