我正在使用这个 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");