Wednesday, June 29, 2011

Beginning Windows Phone 7 Development - Part 3


Working with Isolated Storage

In this part, I’ll explain how you can store basic data on the phone from within your application. Start by creating a new Silverlight for Windows Phone Application project as described in Part 1.

Open the MainPage.xaml file in design view so that you can see the default phone page created. On this screen you will add the following controls to interact with, along with the appropriate labels:

TextBox to type in a string of text you want to save; name it txtData.
Button that you can click to save the text in isolated storage; name it btnSave.
TextBlock to show the saved text next time you run the app; name it tbData.


After adding all these controls, your designer should look like figure 1.

Figure 
1 
Figure 1

Now, we need to setup the event handler for the Save button. You can do this by double-clicking on the Save button in the designer. This will open the code-behind file (MainPage.xaml.cs) and you will see a new method in there called btnSave_Click.

To add the functionality which saves the text entered in isolated storage, you will need to add the following line of code in the event handler for the Save button:

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

      if (settings.Contains("SavedString") == false) //Check if the setting already exists or not.
      {
        settings.Add("SavedString", txtData.Text);
        settings.Save();
      }
      else //If the setting does already exist, update it.
      {
        IsolatedStorageSettings.ApplicationSettings["SavedString"] = txtData.Text;
      }


For all this to work, you will also need to include the System.IO.IsolatedStorage namespace in your MainPage.xaml.cs file using the following syntax:

using System.IO.IsolatedStorage;

This takes care of the saving part, but how about the code to read from isolated storage on application start? That’s next!

We will read from the Isolated Storage on Application Load. In our sample application, we can do so in the Loaded event of the MainPage. You can create an event handler for the Loaded event with the required parameters and the method signature by selecting the PhoneApplicationPage in the design window of MainPage.xaml. In the properties window, select the Events tab, and then add a name for the event in the Loaded textbox, as shown in Figure 2.

Figure 2 
Figure 2


In the newly created page-loaded event handler, type the following lines of code:

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

      if (settings.Contains("SavedString") == true)
        tbData.Text = settings["SavedString"].ToString();
      else
        tbData.Text = "No Saved Data!";


Build the solution using the Build menu and then start debugging either by selecting the Start Debugging option from the Debug menu or by pressing F5.

Once the application is loaded, type in some text in the textbox control and click the Save button to save the text in Isolated Storage. Just clicking the Save button once should save the text in Isolated Storage. Note that in this example, no feedback is provided on screen to indicate that the text for successfully saved. I will leave the user facing messages and exception handling upto you for now.

Now that you have successfully saved some text in Isolated Storage, you should try and view it in the TextBlock you added to your Main screen. To do that, close the app by clicking either the Back button or the Start button on your phone. Now that the application is closed, click the application icon in the phone’s app list. This will start your application again and now you shall see the saved message in the TextBlock on your screen.

Please let me know via the comments below, what other features would you like to know about.

Links to other parts in this series are provided below.

Part 1 - Understanding the development environment
Part 2 - Developing your first Windows Phone App

No comments: