Checkbox not saving state

风格不统一 提交于 2019-12-25 02:18:43

问题


I currently have a checkbox to adhere to MS location services rules, but it doesn't seem to save its state when you revisit the page.

Code is below:

private void cbLocationAllow_Checked(object sender, RoutedEventArgs e)
{
    var settings = IsolatedStorageSettings.ApplicationSettings;

    settings["allowLocation"] = true;
    settings.Save();

}

private void cbLocationAllow_Unchecked(object sender, RoutedEventArgs e)
{
    var settings = IsolatedStorageSettings.ApplicationSettings;

    settings["allowLocation"] = false;
    settings.Save();
}

I thought it would be something like..

   private void SaveState(CheckBox checkBox)
    {

        var settings = IsolatedStorageSettings.ApplicationSettings;

        if (settings.Contains("allowLocation"))
        {
            checkbox.isChecked == true;

        }

But it doesn't seem to work and I'm looking for some help, once again.


回答1:


The way I do it is to create a settings class, and bind the control to the property in the settings class. For simple programs, I use this method even for things that aren't really "settings", but just data that I need to have saved. In a couple cases, I have data that might be changed by another page, so for data that might be changed while not on the original page (in which case the NotifyChanged event doesn't fire because the page isn't in scope), I have code behind that goes back and checks the saved data.

Here's an example of the settings class similar to what I use:

http://msdn.microsoft.com/en-us/library/ff769510%28v=VS.92%29.aspx




回答2:


I don't really understand the problem. but if you do this,

 settings.Save();

then you save the settings. So if you browse through that part again, just load it back. Do not try to change the isChecked function of the checkbox.

or you can try to bind the isChecked of the checkbox to a local boolean variable. then save that in your database. if you load it again you can try adding this before or while loading.

 cbLocationAllow.IsChecked = settings["allowLocation"];

Let me know if you need anything else.




回答3:


Try using Mat laceys Tombstone helper, it makes saving the state of all Silverlight pages very easy to do, in fact in most cases it is just two lines of code per page to do.

WP7 Tombstone helper



来源:https://stackoverflow.com/questions/7765076/checkbox-not-saving-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!