windows phone 7 IndependentStorageSettings.ApplicationSettings 复杂数据 [英] windows phone 7 IsolatedStorageSettings.ApplicationSettings complex data

查看:17
本文介绍了windows phone 7 IndependentStorageSettings.ApplicationSettings 复杂数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个简单的问题.在WP7 中,使用IsolatedStorageSettings.ApplicationSettings 存储复杂数据真的很糟糕吗?我想保存一些类对象的集合.属性用 [DataMember] 属性标记.

Just a quick question. In WP7, is it really bad design/idea to store complex data using IsolatedStorageSettings.ApplicationSettings? I want to save a collection of some class objects. The properties are marked with [DataMember] attributes.

一个类的例子是,

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

我试过了,它奏效了,但我想知道这是否是一种正确的方法,从长远来看是否会影响性能?

I tried it and it worked, but I want to know if it is a correct approach and will there be any performance hit in the long run?

推荐答案

@Jonna.我也考虑过这个.我最终使用/改编了以下通用方法来序列化和反序列化,如下所示使用 IsolatedStorageFile.这包括在您尝试更新数据时删除已存在的文件.

@Jonna. I deliberated over this one too. I ended up using/adapating the following generic methods to serialize and deserialize using a IsolatedStorageFile as below. It includes deleting a file if it already exists as you are trying to update the data.

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

反序列化将被这样调用:

And deserialization would be called thus:

Items = Serialization.Read<measurements>(App.MEASUREMENTS);

user 是一个类,userDetails 是一个基于该类的对象.Measurements 是一个类,Items 是基于该类的对象.App.USERDETAILS &App.MEASUREMENTS 是包含文件名的全局字符串.

user is a class and userDetails is an object based on the that class. Measurements is a class and Items is an object based on that class. App.USERDETAILS & App.MEASUREMENTS are global strings that contain file names.

保留了一些调试行,以便跟踪进度.

Some debug lines have been left in just so progress can be tracked.

如果您正在考虑迁移到 Mango,那么可能也值得考虑使用 SQL + LINQ 并且其中大部分都可以在那里处理...

It might also be worth considering using SQL + LINQ if you are thinking of migrating to Mango and much of this could be taken care of there...

这篇关于windows phone 7 IndependentStorageSettings.ApplicationSettings 复杂数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆