本文介绍了独立存储和放大器;保存多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道最好的方法是创建保存和加载逻辑,这样我可以保存和负荷×物品是什么。例如,在独立存储我可以很容易地这样做节省了复合/ 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数量?会是最好创建一个列表<为MyObject> 采集并保存起来,每当我想保存另一个对象我基本上加载现有的和做的。新增?(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提供到存储在本地IsolatedStorageFile用户专用数据作为键 - 值对的简便方法。一个典型的应用是保存设置,如图像的数量每页显示,页面布局选项,等等。

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);
                }
            }
        }
    }

这篇关于独立存储和放大器;保存多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:24