Windows Phone 7 with MVVM, Isolated Storage and Cloud access – Part II by Mingfei Yan

This is a continuous example from Part I tutorials. If you want to skip Part 1, you could download the project here. And if you want source code and code snippet for the whole project, you could download it here.

–          How to pass data between pages – isolated storage

–          How to use CameraTask in your application

In Part 2, I will introduce two things: Isolated storage and Camera Task.

Before started, I want to introduce why. If you want to pass data from one page to the other, you have to store data somewhere since its stateless between pages. And you could use Isolated Storage. What’s Isolated Storage? Very simple.  It is just storage inside your phone but “Isolated” to only your phone. If you lost your phone, you lost data inside your isolated storage in your phone.

1.You could open app.xaml and add in the following code. App.xaml is a file running before running Mainpage.xaml.
[cc lang=’c’]
//IsolatedStroage Manager
public static class IsolatedStorageCacheManager
{
//Store information into file
public static void Store(string filename, T obj)
{
//create filestream for writing into Isolated Stroage
IsolatedStorageFile appStore = IsolatedStorageFile.GetUserStoreForApplication();

using (IsolatedStorageFileStream fileStream = appStore.OpenFile(filename, FileMode.Create))
{

//Use DataContractSerializer to serialize data and write to the filestream
DataContractSerializer serializer = new DataContractSerializer(typeof(T));

serializer.WriteObject(fileStream, obj);
}
}

//Method for retriving object from file in Isolated Stroage
public static T Retrieve(string filename)
{
T obj = default(T);

IsolatedStorageFile appStore = IsolatedStorageFile.GetUserStoreForApplication();

//Open file if filename exists in Isolated Stroage
if (appStore.FileExists(filename))
{
using (IsolatedStorageFileStream fileStream = appStore.OpenFile(filename, FileMode.Open))
{
//Use Serializer to read filestream and retrieve object
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
obj = (T)serializer.ReadObject(fileStream);
}
}
return obj;
}
}
[/cc]
2. Methods in App.xaml could be used directly by other file in the project. You could just type “App.” to access all the method inside.  We need to serialize data object into a file format that could be stored. There are mainly two ways to do that. Firstly, you could use Build-in DataContractSerialize. It will serialize object based on [DataContract] and [DataMember] these two attributes. The second method is using Linq to XML API from System.XML.Linq namespace.

High level speaking. This IsolatedStroageCacheManager has two methods. One is to store your object into file on Isolated storage. And the other method is to retrieve object from file in Isolated Stroage, with a particular filename you used to save the object.

3. We need to add a reference into the project. Right click on References folder in your project file explorer. Choose System.Xml.Serialization.

4. Add another namespaces in App.xaml to resolve.
[cc lang=’c’]
using System.IO.IsolatedStorage;
using System.IO;
using System.Runtime.Serialization;
[/cc]
5. Add we come back to Mainpage.xaml to implement the following functionality: we will click save button and save whatever customer ordered into isolated storage and retrieve it out from next page (OrderConfirm.xaml).

6. Add this button in MainPage.xaml:
[cc lang=’c’]

Related Posts

Leave a Reply