by November 1, 2010 0 comments



In last month’s article we took a look at how even though there is no real multitasking in Windows Phone 7 to save on battery life, applications get events that allow navigation between screens of the app itself and between apps while saving the data that the user was working on. This gives the impression that the app was running in the background, waiting for the user to come back to it. This data however is not stored if the application is quit completely or the device restarts.

This is where the storage of ‘persistent data’ comes in. Since Windows Phone 7 uses Silverlight as the development platform it can use the built in IsolatedStorage method to store data into a ‘file’. There are a few things you will need to get this to work.

First off, you need to define the ‘structure’ of the data you wish to store — as in what are the ‘fields’ in it. For instance, if you’re creating an app to calculate the EMI of a loan you will need to store the Loan Amount, Rate of Interest and the Tenure in Years as well as the calculated EMI Amount. Simply create a class that has all of these fields and add them to the project. Here is such a sample class:

Note that this class has some connection to property changes. This is used for automatic binding (and therefore calculation) when user enters data into any of the properties. Once this class is added, perform a build and then open the MainPage.XAML file. Add in a few TextBox fields for taking the first three values and a TextBlock for displaying the calculated EMI. Modify the XAML in the following manner.

When you now run the app, you will be able to enter some values within the textboxes and immediately see the EMI get calculated and displayed to you below. But we haven’t started saving and retrieving this yet.
If you recall last month’s article, an app exits when the Back button is pressed beyond the first screen of the app. To handle this, write the following code to save the data when the app is quitting:

 

This code will now display a dialog box informing the user that application is exiting and whether he wishes to save the data or not. If he says yes, the current data in the databound elements will be stored into a Isolated Storage file called MortgageInfo.dat.

To retrieve this data, you need to do it when the application is launching. Simply open the App.XAML.cs file and add the following code in.

This code checks whether a file exists in the Isolated Storage and if yes, loads it and binds it to the Mortage class. When the MainPage loads it finds this instance and fills in the elements with databinding automatically.
As you can see, saving and retrieving data into a Windows Phone 7 application is not difficult at all. With some simple, reusable code you can store any kind of data locally and
read it whenever you wish to do it. Next month we’ll look at some more new features that you can develop for in
Windows Phone 7.

Related Articles:

Develop Apps for Win Phone 7

Managing State in the Application Lifecycle

Developing Windows Phone 7 Application

No Comments so far

Jump into a conversation

No Comments Yet!

You can be the one to start a conversation.

<