I was talking with my good buddy Sam Stokes today about a group of students he is working with who are having some trouble with

saving the state of their applications. He was looking for a way to show them an example on a single page, just to get the concept across. As we often do, we loaded up a screen sharing session and created a new project to build a sample out.

To start with, we wanted to be able to show that the state was being saved, so we dropped a couple of controls onto the page.

MainPageUI

We added event handlers for the OnNavigatedTo, and OnNavigatedFrom events. When we navigate to the page, we'll check to see if anything was saved and load the values. When we navigate away, we'll save everything to IsolatedStorage.

Saving and Loading State
  1. public void SaveStateToIsolatedStorage()
  2. {
  3.     IsolatedStorageSettings isolatedStore = IsolatedStorageSettings.ApplicationSettings;
  4.  
  5.     isolatedStore.Remove("textBox1");
  6.     isolatedStore.Add("textBox1", textBox1.Text);
  7.  
  8.     isolatedStore.Remove("checkBox1");
  9.     isolatedStore.Add("checkBox1", checkBox1.IsChecked.ToString());
  10.  
  11.     isolatedStore.Save();
  12. }
  13.  
  14. public void LoadStateFromIsolatedStorage()
  15. {
  16.     IsolatedStorageSettings isolatedStore = IsolatedStorageSettings.ApplicationSettings;
  17.  
  18.     if (isolatedStore.Contains("textBox1"))
  19.     {
  20.         textBox1.Text = (string)isolatedStore["textBox1"];
  21.     }
  22.  
  23.     if (isolatedStore.Contains("checkBox1"))
  24.     {
  25.         bool isChecked = false;
  26.         if (bool.TryParse((string)isolatedStore["checkBox1"], out isChecked))
  27.         {
  28.             checkBox1.IsChecked = isChecked;
  29.         }
  30.     }
  31. }

Y

 

If you want to take a look at the project in action, you can download the source code.

After we got the project up and going, I was inspired to build another project that shows how to use the application events to support Fast Application Switching. I'll post more on that next.