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存储为字典。所以一般而言,您需要将其明确地转换为您需要使用的任何类型。