Reputation: 87
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
Reputation: 930
For most Windows 8 Store apps, data can largely be classified under two categories:
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
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
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
Reputation: 869
Here you should find all you need: http://msdn.microsoft.com/en-us/library/windows/apps/hh986968.aspx
Upvotes: 4