简体   繁体   中英

How to save the state of CustomObject state in wp7 phone application Page

I have a custom Class and i want to save the Object while deactivating the application,Normal types such as String,int ,was able to save the state and restore it back using the Phone Application Page state.I think ,I should be making myclass as serializable ,So that i will not be able to face the issue while saving the object (MyCustomObject) and restoring the state of the Object.

I tried using System.xml.serialization,and i tried using [DataContract] as suggested in JesseLiberty blog,Again when i try using this ,i got the issue My Net frame work is 2.0 and for that it requires 3.0,I dont know whether it is appropriate or not.

Can anyone help in this issue.

I am using this Helper method and I am able to save different types of data (Custom objects also) in IsolatedStorage and can retrieve them easily.

//Helper method to save a key value pair in ISO store
    internal static void SaveKeyValue<T>(string key, T value)
    {
        if (IsolatedStorageSettings.ApplicationSettings.Contains(key))
            IsolatedStorageSettings.ApplicationSettings[key] = value;
        else
            IsolatedStorageSettings.ApplicationSettings.Add(key, value);
        IsolatedStorageSettings.ApplicationSettings.Save();
    }

//Helper method to load a value of type T associated with the key from ISO store
    internal static T LoadKeyValue<T>(string key)
    {
        if (IsolatedStorageSettings.ApplicationSettings.Contains(key))
            return (T)IsolatedStorageSettings.ApplicationSettings[key];
        else
            return default(T);
    }

And here is the sample usage of these helper methods.

//Save your custom objects whenever you want
SaveKeyValue<MyCustomClass>("customObjectKey", customObject);

//Load your custom objects after the re activation of app..or whenever you need
MyCustomClass customObject = LoadKeyValue<MyCustomClass>("customObjectKey");

The PageState object is just a dictionary<string, object> and it gets serialized to XML.

If you want to store objects in there you'll need to be able to serialize and deserialize them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM