Wednesday, April 14, 2010

Serialization and IsolatedStorage as a data persistency approach for Windows Phone 7

So, We don't have a local db support. What we can do to accomplish a workaround for that ?
First thing I've thought about was Serialization. We can create our objects, Specify them as Serialized and Save them either in binary format (using BinaryFormatter- to store binary data such images) or XML format (using SoapFormatter).
Looking into Windows Phone Application References, you will see that System.Runtime.Serialization is supported. add that and then import into your code class.
  • See that we can't achieve this approach because we don't have:
    1. BinaryFormatter and SoapFormatter
    2. ISerializable interface
Ok, let's try creating POCOS and saving them using IsolatedStorage.
  • add the reference then import System.Runtime.Serialization
  • Define the POCO, like this
    [DataContract]
    public class MyObject
    {
    [DataMember]
    public int P1 {get; set;}

    [DataMember]
    public MyObject2 O1 {get; set;}
    ...........
    }
  • Remember to define MyObject2 as a POCO again
  • after defining all your POCOS, create a Serializer class and try to Save data using IsolatedStorageFile and IsolatedStorageFileStream).
  • Well, I've created a Test UI and this worked for me, but... again, The Emulator didn't persist the data after reloading it.I'm still not sure what's the reason for that, and seems like WP7 Emulator doesn't have an SD card option like Andriod.
The next thing I'm going to try is the creation of a class library for this code and then trying to incorporate it, Or the use of XML and XML Parsing.

No comments:

Post a Comment