保存 List<Object> 的最佳方法是什么?在 Windows 8 应用程序中 [英] What is the best way of saving List<Object> in Windows 8 app

查看:9
本文介绍了保存 List<Object> 的最佳方法是什么?在 Windows 8 应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 List 数据.我想保存它并在每次我的应用程序启动和退出时分别检索它.什么是 Windows 8 中的 IsolatedStorage (WP7) 的等效项.如何保存这些设置?

I have a List<class> of data. And I want to save it and retrieve it every time my app starts and exits respectively. What is the equivalent of IsolatedStorage (WP7) in Windows 8. How can I save these settings?

推荐答案

在 Windows 8 中,您必须为您的应用使用 LocalFolder,您可以使用以下方式访问它:

In windows 8, you have to use the LocalFolder for your app, which you can access using:

StorageFolder folder = ApplicationData.Current.LocalFolder;

然后使用以下方法引用保存在那里的文件:

and then reference files saved there by using:

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

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

I am currently in a similar situation in a project I am working on, where I want to store a List of custom objects to my Apps LocalFolder and have it reloaded later.

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

My solution was to serialize the list to an XML string, and store this in the App Folder. You should be able to adapt my methods:

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:

Now that you have the string you can save it a text file and put this in LocalStorage:

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

现在当你再次加载你的应用程序时,你可以使用上面提到的加载方法从 LocalStorage 中获取 xmlFile,然后将其反序列化以获取你的 List.

Now when you load your app again you can use the loading method mentioned above to get the xmlFile from LocalStorage, and then deserialize it to get your List back.

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

再次,根据您的需要调整它:

Again, adapt this to your needs:

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

这篇关于保存 List&lt;Object&gt; 的最佳方法是什么?在 Windows 8 应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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