在Windows 8应用程序</object>中保存List <object>的最佳方法是什么

时间:2012-10-07 12:25:47

标签: windows-8

我有List<class>个数据。我希望保存它并在每次我的应用程序启动和退出时检索它。 Windows 8中IsolatedStorage(WP7)的等价物是什么?如何保存这些设置?

2 个答案:

答案 0 :(得分:8)

在Windows 8中,您必须使用适用于您的应用的LocalFolder

StorageFolder folder = ApplicationData.Current.LocalFolder;

然后使用以下参考文件保存:

var fileToGet = await folder.GetFileAsync("nameOfFile.fileType");

我目前正处于我正在处理的项目中的类似情况,我希望将自定义对象列表存储到我的Apps LocalFolder并稍后重新加载。

我的解决方案是将列表序列化为XML字符串,并将其存储在App文件夹中。你应该能够调整我的方法:

static public string SerializeListToXml(List<CustomObject> List)
    {
        try
        {
            XmlSerializer xmlIzer = new XmlSerializer(typeof(List<CustomObject>));
            var writer = new StringWriter();
            xmlIzer.Serialize(writer, List);
            System.Diagnostics.Debug.WriteLine(writer.ToString());
            return writer.ToString();
        }

        catch (Exception exc)
        {
            System.Diagnostics.Debug.WriteLine(exc);
            return String.Empty;
        }

现在您已经拥有了该字符串,您可以将其保存为文本文件并将其放在LocalStorage中:

//assuming you already have a list with data called myList
await Windows.Storage.FileIO.WriteTextAsync("xmlFile.txt", SerializeListToXml(myList));

现在,当您再次加载应用程序时,您可以使用上面提到的加载方法从LocalStorage获取xmlFile,然后对其进行反序列化以使List恢复。

string listAsXml = await Windows.Storage.FileIO.ReadTextAsync(xmlFile.txt);
List<CustomObject> deserializedList = DeserializeXmlToList(listAsXml);

再次,根据您的需求进行调整:

public static List<CustomObject> DeserializeXmlToList(string listAsXml)
    {
        try
        {
            XmlSerializer xmlIzer = new XmlSerializer(typeof(List<CustomObject>));
            XmlReader xmlRead = XmlReader.Create(listAsXml);
            List<CustomObject> myList = new List<CustomObject>();
            myList = (xmlIzer.Deserialize(xmlRead)) as List<CustomObject>;
            return myList;
        }

        catch (Exception exc)
        {
            System.Diagnostics.Debug.WriteLine(exc);
            List<CustomObject> emptyList = new List<CustomObject>();
            return emptyList;
        }
    }

答案 1 :(得分:5)

您可以使用此类来存储和加载设置:

public static class ApplicationSettings
{
    public static void SetSetting<T>(string key, T value, bool roaming = true)
    {
        var settings = roaming ? ApplicationData.Current.RoamingSettings : ApplicationData.Current.LocalSettings;
        settings.Values[key] = value;
    }

    public static T GetSetting<T>(string key, bool roaming = true)
    {
        return GetSetting(key, default(T), roaming);
    }

    public static T GetSetting<T>(string key, T defaultValue, bool roaming = true)
    {
        var settings = roaming ? ApplicationData.Current.RoamingSettings : ApplicationData.Current.LocalSettings;
        return settings.Values.ContainsKey(key) &&
               settings.Values[key] is T ?
               (T)settings.Values[key] : defaultValue;
    }

    public static bool HasSetting<T>(string key, bool roaming = true)
    {
        var settings = roaming ? ApplicationData.Current.RoamingSettings : ApplicationData.Current.LocalSettings;
        return settings.Values.ContainsKey(key) && settings.Values[key] is T;
    }

    public static bool RemoveSetting(string key, bool roaming = true)
    {
        var settings = roaming ? ApplicationData.Current.RoamingSettings : ApplicationData.Current.LocalSettings;
        if (settings.Values.ContainsKey(key))
            return settings.Values.Remove(key);
        return false;
    }
}

但是你只能保存和加载原始类型(bool,int,string等)。这就是您必须将列表序列化为XML或其他可以存储在字符串中的格式的原因。要将对象序列化和反序列化为XML,您可以使用以下方法:

public static string Serialize(object obj)
{
    using (var sw = new StringWriter()) 
    {
        var serializer = new XmlSerializer(obj.GetType());
        serializer.Serialize(sw, obj);
        return sw.ToString();
    }
}

public static T Deserialize<T>(string xml)
{
    using (var sw = new StringReader(xml))
    {
        var serializer = new XmlSerializer(typeof(T));
        return (T)serializer.Deserialize(sw);
    }
}

另见Is there a way to store instances of own classes in the ApplicationSettings of a Windows Store app?