How to save flag state

How to save checkbox state in Visual Studio in 2012 using C #. Is this even after the application is completed, when I open it again, should the last viewed state be visible? what to do? Could you help me with the code.

Thanks in advance.

J. visali

+4
source share
4 answers
+4
source

For most Windows 8 Store applications, data can be largely divided into two categories:

  • Application data - settings, files, cache, session state, user lists, etc.
  • User data - documents, credentials, photos, music, etc.

This article, Data Saving and Application Lifecycle Management, should help you understand what type of persistence you need. Data can be stored as application parameters in key-value pairs, but can also be saved as application files, which are usually stored in access to files with an isolated file system that applications have.

As a quick example, you should have:

// 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"]); 

Edit

Tested example that works:

  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; } 

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

+3
source

You can use the local storage provided by the application:

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

Then you can restore using the same localSettings , extracting state from the values.

+1
source

Application settings are the best way to save application data ... the best solution for http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdata.localsettings

+1
source

All Articles