2

我在這一切的C#的Windows Phone編程很新,所以這是最有可能一個愚蠢的問題,但我需要知道anywho ...驗證應用程序設置爲Windows Phone應用程序的獨立存儲鍵值

  IsolatedStorageSettings appSettings = 
       IsolatedStorageSettings.ApplicationSettings; 

     if (!appSettings.Contains("isFirstRun")) 
     { 
      firstrunCheckBox.Opacity = 0.5; 

      MessageBox.Show("isFirstRun not found - creating as true"); 

      appSettings.Add("isFirstRun", "true"); 
      appSettings.Save(); 
      firstrunCheckBox.Opacity = 1; 
      firstrunCheckBox.IsChecked = true; 
     } 
     else 
     { 
      if (appSettings["isFirstRun"] == "true") 
      { 
       firstrunCheckBox.Opacity = 1; 
       firstrunCheckBox.IsChecked = true; 
      } 
      else if (appSettings["isFirstRun"] == "false") 
      { 
       firstrunCheckBox.Opacity = 1; 
       firstrunCheckBox.IsChecked = false; 
      } 
      else 
      { 
       firstrunCheckBox.Opacity = 0.5; 
      } 
     }   

我試圖首先檢查我的應用程序設置隔離存儲中是否有特定的鍵,然後希望使CheckBox顯示爲選中狀態或取消選中狀態,具體取決於該鍵的值是「true」還是「false」。此外,我沒有采取任何行動時默認複選框的不透明度爲0.5不透明度。

隨着我的代碼,我得到的警告

可能出現的意外參考比較;爲了得到一個值的比較,把左邊的類型'串'

有人可以告訴我我做錯了什麼。我已經探索了將數據存儲在獨立存儲txt文件中,並且工作正常,我現在正在嘗試應用程序設置,並最終嘗試下載和存儲xml文件,以及創建用戶設置並將其存儲到xml文件中。我想嘗試瞭解所有向我敞開的選項,並使用其以往運行更好,更快

回答

3

如果你明確地投在的appSettings值檢索的結果字符串是這樣的:

 if ((string)appSettings["isFirstRun"] == "true") 
     { 
      firstrunCheckBox.Opacity = 1; 
      firstrunCheckBox.IsChecked = true; 
     } 
     else if ((string)appSettings["isFirstRun"] == "false") 
     { 
      firstrunCheckBox.Opacity = 1; 
      firstrunCheckBox.IsChecked = false; 
     } 
     else 
     { 
      firstrunCheckBox.Opacity = 0.5; 
     } 

和這將使警告消失。

+0

非常感謝,我現在就開始工作。明天我將繼續嘗試XML,並學習Xelement。 如果我能得到所有的工作,我可以開始構建我的第一個適當的應用程序,我認爲Windows Phone的某種天氣應用程序。 – 2010-03-23 02:37:44

+0

@馬丁安德森,我很高興它爲你工作。 – 2010-03-23 03:28:57

+0

@Martin:看到這個答案旁邊那個大的空白記號?點擊它。 ;) – AnthonyWJones 2010-03-23 17:08:43

1

IsolatedStorageSettings存儲爲字典。所以一般而言,您需要將其明確地轉換爲您需要使用的任何類型。