有没有办法在Windows应用商店应用的ApplicationSettings中存储自己的类的实例? [英] Is there a way to store instances of own classes in the ApplicationSettings of a Windows Store app?

查看:183
本文介绍了有没有办法在Windows应用商店应用的ApplicationSettings中存储自己的类的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows应用商店应用中,根据文档,我只能将WinRT类型存储在ApplicationSettings中。对于应该一起保存的漫游设置,我可以使用ApplicationDataCompositeValue。尝试存储自己的类或结构的实例会导致出现消息WinRT信息:尝试序列化要写入应用程序数据存储的值时出错的异常。附加信息:不支持此类型的数据。术语尝试序列化表示必须有某种方式使应用数据API的类型序列化。

In a Windows Store app I can only store WinRT types in the ApplicationSettings, according to the documentation. For roamed settings that should be held together I can use ApplicationDataCompositeValue. Trying to store an instance of an own class or struct results in an Exception with the message " WinRT information: Error trying to serialize the value to be written to the application data store. Additional Information: Data of this type is not supported". The term "trying to serialize" indicates that there must be some way so serialize a type for the application data API.

有没有人知道我该如何实现?

Does anyone know how I could achieve that?

我试过DataContract序列化,但没有工作。 / p>

I tried DataContract serialization but it did not work.

推荐答案

我认为不支持自定义/自己的类型。

I think custom/own types are not supported.

查看 http://msdn.microsoft.com/en-us/library/windows/apps/hh464917.aspx
Windows Runtime数据类型支持应用程序设置。

See http://msdn.microsoft.com/en-us/library/windows/apps/hh464917.aspx: "The Windows Runtime data types are supported for app settings."

但是你可以将对象序列化为XML并保存为字符串...(见下面的代码)

But you can serialize your objects to XML and save as string... (see code below)

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

https://github.com/MyToolkit/MyToolkit/blob/master/src/MyToolkit/Serialization/XmlSerialization。 cs

也请查看此类别:

https://github.com/MyToolkit/MyToolkit/wiki/XmlSerialization

免责声明:以上链接来自我的项目

这篇关于有没有办法在Windows应用商店应用的ApplicationSettings中存储自己的类的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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