Windows phone 7 windows phone 7隔离存储设置。应用程序设置复杂数据
只是一个简单的问题。在WP7中,使用IsolatedStorageSettings.ApplicationSettings存储复杂数据的设计/想法真的很糟糕吗?我想保存一些类对象的集合。属性用[DataMember]属性标记 一个类的例子是Windows phone 7 windows phone 7隔离存储设置。应用程序设置复杂数据,windows-phone-7,Windows Phone 7,只是一个简单的问题。在WP7中,使用IsolatedStorageSettings.ApplicationSettings存储复杂数据的设计/想法真的很糟糕吗?我想保存一些类对象的集合。属性用[DataMember]属性标记 一个类的例子是 [DataContract] public class OfflineItem { [DataMember] public string Id { get; set; } [DataMember] public MyItem
[DataContract]
public class OfflineItem
{
[DataMember]
public string Id { get; set; }
[DataMember]
public MyItem Item { get; set; }
[DataMember]
public Dictionary<string, string> KeyValues { get; set; }
}
Collection<OfflineItems> offlineItems = new Collection<OfflineItems>();
.....
IsolatedStorageSettings.ApplicationSettings["AllOfflineItems"] = offlineItems;
我尝试了它,它成功了,但我想知道它是否是一种正确的方法,从长远来看会不会对性能造成影响?我会将我的数据序列化为XML或二进制文件,并保存到IsolatedStorage中的一个单独文件中。因为如果IsolatedStorageSettings.ApplicationSettings过于拥挤,则加载任何单个设置都需要更长的时间 下面是一个将对象序列化为xml的通用方法
public static string SerializeXml(object objectToSerialize)
{
using (var ms = new MemoryStream())
{
var serializer = new XmlSerializer(objectToSerialize.GetType());
serializer.Serialize(ms, objectToSerialize);
ms.Position = 0;
using (var reader = new StreamReader(ms))
{
return reader.ReadToEnd();
}
}
}
我将把我的数据序列化为XML或二进制文件,并保存到IsolatedStorage中的一个单独文件中。因为如果IsolatedStorageSettings.ApplicationSettings过于拥挤,则加载任何单个设置都需要更长的时间 下面是一个将对象序列化为xml的通用方法
public static string SerializeXml(object objectToSerialize)
{
using (var ms = new MemoryStream())
{
var serializer = new XmlSerializer(objectToSerialize.GetType());
serializer.Serialize(ms, objectToSerialize);
ms.Position = 0;
using (var reader = new StreamReader(ms))
{
return reader.ReadToEnd();
}
}
}
@乔娜。我也仔细考虑过这个问题。最后,我使用/adapating以下通用方法来使用IsolatedStorageFile进行序列化和反序列化,如下所示。它包括在您尝试更新数据时删除已存在的文件
internal static void Write<T>(T obj, string fileName)
{
XmlWriterSettings writerSettings = new XmlWriterSettings
{
Indent = true,
IndentChars = "\t"
};
try
{
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isoStore.FileExists(fileName))
{
isoStore.DeleteFile(fileName);
}
using (var isoStream = new IsolatedStorageFileStream(fileName, FileMode.Create, isoStore))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (XmlWriter xmlWriter = XmlWriter.Create(isoStream, writerSettings))
{
serializer.Serialize(xmlWriter, obj);
}
}
}
}
catch (IsolatedStorageException ex)
{
Debug.WriteLine(ex.Message);
}
catch (Exception emAll)
{
Debug.WriteLine(emAll.Message);
}
}
internal static T Read<T>(string fileName)
{
try
{
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var isoStream = new IsolatedStorageFileStream(fileName, FileMode.Open, isoStore))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(isoStream);
}
}
}
catch (IsolatedStorageException ex)
{
Debug.WriteLine(ex.Message);
throw;
}
catch (Exception emAll)
{
Debug.WriteLine(emAll.Message);
throw;
}
}
因此,序列化将被称为:
Serialization.Write<user>(userDetails, App.USERDETAILS);
Items = Serialization.Read<measurements>(App.MEASUREMENTS);
反序列化将被称为:
Serialization.Write<user>(userDetails, App.USERDETAILS);
Items = Serialization.Read<measurements>(App.MEASUREMENTS);
user是一个类,userDetails是基于该类的对象。Measurements是一个类,Items是基于该类的对象。App.USERDETAILS和App.measures是包含文件名的全局字符串
一些调试行被保留,以便跟踪进度
如果您正在考虑迁移到Mango,那么使用SQL+LINQ也可能是值得考虑的,而且大部分问题都可以在那里解决……@Jonna。我也仔细考虑过这个问题。最后,我使用/adapating以下通用方法来使用IsolatedStorageFile进行序列化和反序列化,如下所示。它包括在您尝试更新数据时删除已存在的文件
internal static void Write<T>(T obj, string fileName)
{
XmlWriterSettings writerSettings = new XmlWriterSettings
{
Indent = true,
IndentChars = "\t"
};
try
{
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isoStore.FileExists(fileName))
{
isoStore.DeleteFile(fileName);
}
using (var isoStream = new IsolatedStorageFileStream(fileName, FileMode.Create, isoStore))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (XmlWriter xmlWriter = XmlWriter.Create(isoStream, writerSettings))
{
serializer.Serialize(xmlWriter, obj);
}
}
}
}
catch (IsolatedStorageException ex)
{
Debug.WriteLine(ex.Message);
}
catch (Exception emAll)
{
Debug.WriteLine(emAll.Message);
}
}
internal static T Read<T>(string fileName)
{
try
{
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var isoStream = new IsolatedStorageFileStream(fileName, FileMode.Open, isoStore))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(isoStream);
}
}
}
catch (IsolatedStorageException ex)
{
Debug.WriteLine(ex.Message);
throw;
}
catch (Exception emAll)
{
Debug.WriteLine(emAll.Message);
throw;
}
}
因此,序列化将被称为:
Serialization.Write<user>(userDetails, App.USERDETAILS);
Items = Serialization.Read<measurements>(App.MEASUREMENTS);
反序列化将被称为:
Serialization.Write<user>(userDetails, App.USERDETAILS);
Items = Serialization.Read<measurements>(App.MEASUREMENTS);
user是一个类,userDetails是基于该类的对象。Measurements是一个类,Items是基于该类的对象。App.USERDETAILS和App.measures是包含文件名的全局字符串
一些调试行被保留,以便跟踪进度
如果您正在考虑迁移到Mango,那么使用SQL+LINQ也可能是值得考虑的,并且其中的大部分可以在那里解决