如何保存复选框的状态

时间:2013-01-27 20:45:15

标签: c# windows-8 microsoft-metro windows-store-apps checkbox

如何使用C#在Visual Studio Ultimate 2012中保存复选框的状态。即使在我再次重新打开应用程序终止后,最后查看的状态应该可见?该怎么办?请你帮我解释一下代码。

提前致谢.....

J.Visali

4 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:3)

对于大多数Windows 8商店应用,数据可以大致分为两类:

  1. 应用数据 - 设置,文件,缓存,会话状态,自定义列表等。
  2. 用户数据 - 文档,凭据,照片,音乐等。
  3. 这篇文章 Data Persistence & Application Life-Cycle Management应该可以帮助您了解您需要什么类型的持久性。 数据可以作为应用程序设置保存在键值对中,但也可以保存为应用程序文件,它通常存储在应用程序具有的沙盒文件系统访问中

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

       // 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"/>

答案 2 :(得分:1)

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

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

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

答案 3 :(得分:1)

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