BANGALORE, INDIA: 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.
{#PageBreak#}
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.
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:
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.
{#PageBreak#}
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.