2013-02-15 82 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