Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

c# - Isolated Storage & Saving Multiple Objects

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

And load like this:

var settings = IsolatedStorageSettings.ApplicationSettings;
return settings["key"] as MyObject;

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?

So something like this:

List<MyObject> myObjects = new List<MyObject>();
myObjects.Add(newObject);
settings.Add("myObjects", myObjects);

and when Loading:

var myObjects = settings["myObjects"] as List<MyObject>;

This would however require deleting and adding the collection back in as settings.Add requires a unique key. Would this be the best way?

I'd much rather use settings than a Iso File.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...