如何保存复选框的状态

本文关键字:复选框 状态 保存 何保存 | 更新日期: 2023-09-27 17:56:10

如何使用 C# 保存 Visual Studio ultimate 2012 中复选框的状态。即使在应用程序终止后,当我再次重新打开时,上次查看的状态应该可见吗?怎么办?你能帮我写代码吗?

提前感谢.....

J.维萨利

如何保存复选框的状态

在这里你应该找到你需要的一切:http://msdn.microsoft.com/en-us/library/windows/apps/hh986968.aspx

对于大多数 Windows 8 应用商店应用,数据在很大程度上可以分为两类:

  1. 应用程序数据 – 设置、文件、缓存、会话状态、自定义列表等。
  2. 用户数据 – 文档、凭据、照片、音乐等。

本文数据持久性和应用程序生命周期管理应该可以帮助您了解需要哪种类型的持久性。数据可以作为键值对中的应用程序设置保留,但也可以保存为应用程序文件,该文件通常存储在应用程序具有的沙盒文件系统访问权限中。

作为一个简单的例子,你应该有:

   // Reference to Local Application Settings.
   Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; 
   // Reference to Roaming Application Settings.
   Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;  
   // Persisting simple Application Settings.
   localSettings.Values["myOption1"] = myBox1.isChecked;
   roamingSettings.Values["myOption2"] = myBox2.isChecked;
   // Reading settings back.
   var mySavedOption1 = (bool)(localSettings.Values["myOption1"]);
   var mySavedOption2 = (bool)(roamingSettings.Values["myOption2"]);

编辑

经过测试的示例:

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        // Reference to Local Application Settings.
        Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
        // Reading settings back.
        object mySavedOption1;
        localSettings.Values.TryGetValue("myOption1Key", out mySavedOption1);
        if (mySavedOption1 != null)
            myOption1.IsChecked = (bool)mySavedOption1;
    }
    private void myOption1_Checked_1(object sender, RoutedEventArgs e)
    {
        // Reference to Local Application Settings.
        Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
        // Persisting simple Application Settings.
        localSettings.Values["myOption1Key"] = myOption1.IsChecked;
    }

在 xaml 中:<CheckBox x:Name="myOption1" Grid.Row="1" Checked="myOption1_Checked_1" Unchecked="myOption1_Checked_1"/>

您可以使用应用提供的本地存储:

Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
localSettings.Values["CheckboxState"] = "checked";

然后,可以通过从值中提取状态来使用相同的localSettings进行还原。

应用程序设置是存储应用程序数据的最佳方式...最佳解决方案http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdata.localsettings