How to save the state of a checkbox

How to save the state of a checkbox in Visual studio ultimate 2012 using C#. That is even after the app is terminated when I reopen again the last viewed state should be visible? what to do? Could u please help me with the code.

Thanks in advance.....

J.Visali

Upvotes: 3

Views: 4825

Answers (4)

Bianca Daniciuc
Bianca Daniciuc

Reputation: 930

For most Windows 8 Store apps, data can largely be classified under two categories:

  1. App Data – settings, files, cache, session state, custom lists etc.
  2. User Data – documents, credentials, photos, music etc.

This article Data Persistence & Application Life-Cycle Management should help you understand what type of persistence do you need. Data can be persisted, as Application Settings in key-value pairs, but can also be saved as Application Files, which is usually stored in the sandboxed file system access that Apps 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"/>

Upvotes: 3

coders_org
coders_org

Reputation: 19

apps setting is the best way to store the app data... the best solution for the http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdata.localsettings

Upvotes: 1

Konstantin Dinev
Konstantin Dinev

Reputation: 34895

You can use the local storage provided by the app:

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

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

Upvotes: 1

Sven Kannenberg
Sven Kannenberg

Reputation: 869

Here you should find all you need: http://msdn.microsoft.com/en-us/library/windows/apps/hh986968.aspx

Upvotes: 4

Related Questions