4

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

提前致谢.....

维萨里

4

4 回答 4

4

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

于 2013-01-27T20:49:44.543 回答
3

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

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

这篇文章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;

    }

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

于 2013-01-27T21:10:56.770 回答
1

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

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

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

于 2013-01-27T20:49:53.820 回答
1

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

于 2013-01-27T21:30:38.697 回答