在Windows 8.1应用程序页面的代码中,我创建了一个SerializableDictionary:
private static SerializableDictionary<string, Persona> elenco =
new SerializableDictionary<string, Persona>();
其中,SerializableDictionary
类为this。如何使用Windows.Storage.ApplicationData
对象及其RoamingSettings
属性保存elenco
?(如果有帮助的话,甚至可以更改声明private static
)。
好吧,既然它是可序列化的,我想一种方法是将它序列化为字符串,并将该字符串存储在RoamingSettings中。类似这样的东西:
var stringBuilder = new StringBuilder();
var xmlWriter = XmlWriter.Create(stringBuilder);
elenco.WriteXml(xmlWriter);
var applicationData = Windows.Storage.ApplicationData.Current;
applicationData.RoamingSettings.Values["elenco"] = stringBuilder.ToString();
不过,我实际上还没有使用RoamingSettings,所以我可能还差得很远。以下是你如何读取数据的方法:
var applicationData = Windows.Storage.ApplicationData.Current;
var elencoXml = (string)applicationData.RoamingSettings.Values["elenco"];
var xmlReader = XmlReader.Create(new StringReader(elencoXml));
elenco.ReadXml(xmlReader);