隔离存储保存多个对象 [英] Isolated Storage & Saving Multiple Objects
问题描述
我想知道创建保存和加载逻辑的最佳方法是什么,以便我可以保存和加载 x 个项目.例如,在隔离存储中,我可以通过执行以下操作轻松保存复合/POCO 对象:
I wish to know what the best way is to create Saving and Loading logic so that I can save and load x items. For example, in Isolated Storage I can easily save a composite/POCO object by doing this:
var settings = IsolatedStorageSettings.ApplicationSettings;
settings.Add("key", myObject);
并像这样加载:
var settings = IsolatedStorageSettings.ApplicationSettings;
return settings["key"] as MyObject;
但是我如何从IsolatedStorage 加载x 数量的对象?最好创建一个 List
集合并保存,每当我想保存另一个对象时,我基本上加载现有对象并执行 .Add(newObject)
并保存又来了?
But how would I load x amount of Objects from IsolatedStorage? Would it be best to create a List<MyObject>
collection and save and whenever I want to save another object I basically load the existing and do .Add(newObject)
and save again?
就像这样:
List<MyObject> myObjects = new List<MyObject>();
myObjects.Add(newObject);
settings.Add("myObjects", myObjects);
和加载时:
var myObjects = settings["myObjects"] as List<MyObject>;
然而,这需要删除并重新添加集合,因为 settings.Add
需要一个唯一的键.这是最好的方法吗?
This would however require deleting and adding the collection back in as settings.Add
requires a unique key. Would this be the best way?
我更愿意使用设置而不是 Iso 文件.
I'd much rather use settings than a Iso File.
推荐答案
由于 MSDN:IsolatedStorageSettings 提供了一种将用户特定数据作为键值对存储在本地孤立存储文件中的便捷方法.一个典型的用途是保存设置,例如每页显示的图像数量、页面布局选项等.
Due to MSDN : IsolatedStorageSettings provide a convenient way to store user specific data as key-value pairs in a local IsolatedStorageFile. A typical use is to save settings, such as the number of images to display per page, page layout options, and so on.
所以我认为使用IsolatedStorageSettings 不是你最好的选择,如果我是你,我会使用IsolatedStorageFile.
so I don't think that using IsolatedStorageSettings would be your best option , if I were you , I would use IsolatedStorageFile.
要保存和加载列表的内容,场景是
To save and load the content of your list , the scenario would be
1- 如果在您的列表中添加或删除了一个项目,您可以将列表 searlize 为 xml 并将其保存为 IsolatedStorageFile
1- if an item is added or removed from your list , you searlize the list to xml and save it IsolatedStorageFile
private static void Serialize(string fileName, object source)
{
var userStore = IsolatedStorageFile.GetUserStoreForApplication();
using (var stream = new IsolatedStorageFileStream(fileName, FileMode.Create, userStore))
{
XmlSerializer serializer = new XmlSerializer(source.GetType());
serializer.Serialize(stream, source);
}
}
2- 当你想在任何地方加载你的列表时,你需要反序列化存储在 IsolatedStorageFile 中的 xml 文件
2- When you want to load your list at any place , you would deserialize the xml file stored in IsolatedStorageFile
public static void Deserialize<T>(ObservableCollection<T> list , string filename)
{
list = new ObservableCollection<T>();
var userStore = IsolatedStorageFile.GetUserStoreForApplication();
if (userStore.FileExists(filename))
{
using (var stream = new IsolatedStorageFileStream(filename, FileMode.Open, userStore))
{
XmlSerializer serializer = new XmlSerializer(list.GetType());
var items = (ObservableCollection<T>)serializer.Deserialize(stream);
foreach (T item in items)
{
list.Add(item);
}
}
}
}
这篇关于隔离存储保存多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!