如何在Windows Phone应用程序中保存复选框的值

时间:2014-01-23 19:53:39

标签: c# windows-phone-8 visual-studio-2013

即使我关闭应用程序或从一个页面移动到另一个页面,我也试图获取我已经检查过的复选框。

有人能帮助我吗?

2 个答案:

答案 0 :(得分:1)

XAML中的一种快速方式是:

    <CheckBox x:Name="CheckBox1" Checked="CheckClick" Unchecked="UncheckClick" />

在代码背后:

    public MainPage()
    {
        InitializeComponent();
        Loaded += MainPage_Loaded;

    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        var settings = IsolatedStorageSettings.ApplicationSettings;
        if (settings.Contains("CheckBox1Checked"))
            CheckBox1.Checked = settings["CheckBox1Checked"];
    }

    private void OnChecked(object sender, RoutedEventArgs e)
    {
        var settings = IsolatedStorageSettings.ApplicationSettings;
        settings["CheckBox1Checked"] = true;
        settings.Save();
    }


    private void OnUnchecked(object sender, RoutedEventArgs e)
    {
        var settings = IsolatedStorageSettings.ApplicationSettings;
        settings["CheckBox1Checked"] = false;
        settings.Save();
    }

最好是使用MVVM和像

这样的绑定
    <CheckBox IsChecked="{Binding IsCheckbox1Checked" />

答案 1 :(得分:0)

另一种方法是让你关心的是使用String Settings例如

    private void checkBox7_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox7.Checked)
        {
            MySettings.Default.WSites = "1";
            MySettings.Default.Save();
        }
        if (!checkBox7.Checked)
        {
            MySettings.Default.WSites = "";
            MySettings.Default.Save();
        }
    }

然后在论坛加载

        if (MySettings.Default.WSites == "1")
        {
            checkBox11.Checked = true;
        }