开发者

Validating Application Settings Key Values in Isolated Storage for Windows Phone Applications

开发者 https://www.devze.com 2022-12-24 20:15 出处:网络
I am very new at all this c# Windows Phone programming, so this is most probably a dumb question, but I need to know anywho...

I am very new at all this c# Windows Phone programming, so this is most probably a dumb question, but I need to know anywho...

            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;
            }
        }          

I am trying to firstly check if there is a specific key in my Application Settings Isolated Storage, and then wish to make a CheckBox appear checked or unchecked depending on if the value for that key is "true" or "false". Also I am defaulting the opacity of the checkbox to 0.5 opacity when no action is taken upon it.

With the code I have, I get the warnings

Possible unintended reference comparison; to get a value comparison, cast the left hand side to type 'string'

Can someone tell me what I am doing wrong. I have explored storing data in an Isolated Storage txt file, and that worked, I am now trying Application Settings, and will finally try to download and store an xml file, as well as create and store user settings into an xml file. I want to try understand all the options open to me, and use which ever runs better and quicker


If you explicitly cast the results of the value retrieval from the appSettings to string like this:

        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;
        }

and that will make the warnings go away.


IsolatedStorageSettings are stored as Dictionary. So in general you would need to cast it explicitly to whatever type you need to use.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号