[英]How to save the state of CustomObject state in wp7 phone application Page
我有一个自定义的类,我想在停用应用程序时保存对象,诸如String,int之类的普通类型能够保存状态并使用Phone Application Page状态将其还原回去。我认为,我应该将myclass做为作为可序列化的,这样在保存对象(MyCustomObject)和恢复对象状态时我将无法面对问题。
我尝试使用System.xml.serialization,并尝试按照JesseLiberty博客中的建议使用[DataContract],再次尝试使用此方法时,却遇到了问题:我的网络框架是2.0,为此它需要3.0,我不知道是否是否合适。
任何人都可以帮助解决这个问题。
我正在使用此Helper方法,并且能够将不同类型的数据(也包括自定义对象)保存在IsolatedStorage中,并且可以轻松地检索它们。
//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);
}
这是这些辅助方法的示例用法。
//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");
PageState
对象只是一个dictionary<string, object>
,它被序列化为XML。
如果要在其中存储对象,则需要能够对其进行序列化和反序列化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.