2011-09-08 86 views
-1

我想將數據存儲在下拉列表中和2個單選按鈕之間我必須在Window phone 7中使用獨立存儲器存儲一個值a 如何在Wp7中執行獨立存儲?如何在Wp7中實現獨立存儲?

+2

您是否閱讀過關於獨立存儲的各種教程和頁面?你的問題目前太模糊了。 –

+0

我剛寫了一個免費的獨立存儲DLL。非常容易使用http://www.anthonyrussell.info/postpage.php?name=47 –

回答

0

和正常的Silverlight一樣(除了不存在的網站存儲)。有關更多信息,請參閱任何教程/書籍。

6

最簡單辦法做到這一點,是使用IsolatedStorageSettings.ApplicationSettings類/屬性 - 它是一個必須存活無論發生什麼你所有的小的臨時數據有很大DUMPBIN。通常情況下,該對象會自動從ISO存儲中恢復,但要小心以爲 - 如果您的應用程序已正常關閉,則可以使用該對象。如果您想防範例如應用程序崩潰/等 - 您仍然應該定期手動調用此對象上的SAVE。

一些鏈接/ TUTS: MSDN:相當不錯解釋一個例子:http://msdn.microsoft.com/en-us/library/cc221360(v=vs.95).aspx 從谷歌http://dotnet.dzone.com/articles/using-application-settings

5

第一whetever這是我使用的加載和在WP7應用程序保存高分的代碼片段,調整它以滿足您的需求。它可以挽救數百萬人的生命:d

private void LoadHighScore() 
    { 
     // open isolated storage, and load data from the savefile if it exists. 
#if WINDOWS_PHONE 
     using (IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
#else 
     using (IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForDomain()) 
#endif 
     { 
      if (savegameStorage.FileExists("guessthecard.txt")) 
      { 
       using (IsolatedStorageFileStream fs = savegameStorage.OpenFile("guessthecard.txt", System.IO.FileMode.Open)) 
       { 
        if (fs != null) 
        { 
         // Reload the saved high-score data. 
         byte[] saveBytes = new byte[4]; 
         int count = fs.Read(saveBytes, 0, 4); 
         if (count > 0) 
         { 
          highScore = System.BitConverter.ToInt32(saveBytes, 0); 
         } 
        } 
       } 
      } 
     } 
    } 

    // Save highscore 

    public async void UnloadContent() 
    { 
     // SAVE HIGHSCORE 
     // Save the game state (in this case, the high score). 
#if WINDOWS_PHONE 
     IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForApplication(); 
#else 
     IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForDomain(); 
#endif 

     // open isolated storage, and write the savefile. 
     IsolatedStorageFileStream fs = null; 
     using (fs = savegameStorage.CreateFile("guessthecard.txt")) 
     { 
      if (fs != null) 
      { 
       // just overwrite the existing info for this example. 
       byte[] bytes = System.BitConverter.GetBytes(highScore); 
       fs.Write(bytes, 0, bytes.Length); 
      } 
     } 

     try 
     { 
      CardGuess item = new CardGuess { Text = highScore.ToString() }; 
      await App.MobileService.GetTable<CardGuess>().InsertAsync(item); 
     } 
     catch(Exception e) 
     { 
     } 



    } 
0

觀看此錄象..它的過程非常清楚 http://msdn.microsoft.com/en-us/gg241265

+0

請注意,[僅限鏈接回答](http://meta.stackoverflow.com/tags/link-only-answers/info),所以SO答案應該是搜索解決方案的終點(vs.而另一個引用的中途停留時間往往會隨着時間推移而過時)。請考慮在此添加獨立的摘要,並將鏈接保留爲參考。 – kleopatra

0

使用這個類的獨立存儲 公共類的applicationSettings解釋 {/// ///方法得到的值給出關鍵 /// /// /// /// 公共靜態牛逼GetKeyValue(字符串鍵) { 嘗試 if(IsolatedStorageSettings.ApplicationSettings.Contains(key)) return(T)IsolatedStorageSettings.ApplicationSettings [key]; else return default(T); } catch(Exception){return default(T); }} /// /// 方法來設置鍵 /// /// /// /// 公共靜態無效SetKeyValue(字符串鍵,T值) { 如果值( IsolatedStorageSettings.ApplicationSettingsContains(key)) IsolatedStorageSettings.ApplicationSettings [key] = value; else IsolatedStorageSettings.ApplicationSettings.Add(key,value);

 IsolatedStorageSettings.ApplicationSettings.Save(); 
    } 
    /// <summary> 
    /// method to remove key from isolated storage 
    /// </summary> 
    /// <param name="key"></param> 
    public static void RemoveKey(string key) 
    { 
     try 
     { 
      IsolatedStorageSettings.ApplicationSettings.Remove(key); 
     } 
     catch 
     { 
     } 
    } 
    /// <summary> 
    /// method to check when a key exists in isolated storage 
    /// </summary> 
    /// <param name="key"></param> 
    /// <returns></returns> 
    public static bool HasKey(string key) 
    { 
     try 
     { 
      if (IsolatedStorageSettings.ApplicationSettings.Contains(key)) 
       return true; 
      else 
       return false; 
     } 
     catch (Exception) { return false; } 
    } 

} 

然後數據存儲爲 ApplicationSettings.SetKeyValue( 「鑰匙」,值); 檢索 var aaa = ApplicationSettings.GetKeyValue(「key」);