在Windows应用商店应用中恢复状态时出现序列化错误 [英] Serialization error when restoring state in Windows Store App
问题描述
大家好,
尝试序列化我的应用程序数据时,在Windows应用商店应用中出现以下错误:
< pre lang =xml> {类型' MyObject '具有数据合同名称''不是预期的。考虑使用DataContractResolver或添加任何静态未知的类型已知类型的列表 - 例如,通过使用KnownTypeAttribute属性或将它们添加到传递给DataContractSerializer的已知类型列表。}
我已经尝试使用相关标签来装饰相关类,方法如下:
[DataContract]
public class SimpleObject
{
[DataMember]
private string _Name;
[DataMember]
public string 名称
{
// 设置人名
set {此 ._ Name = value ; }
// 获取人名
获取 {返回 此 ._名称; }
}
}
除此之外,在 SuspensionManager
类我将自定义对象显式添加到 SaveAsync()中的
方法如下: _knownTypes
列表中
_ knownTypes.Add( typeof (SimpleObject)) ;
但是,我仍然收到此错误。有没有人有任何建议?
在研究这个问题时我已经阅读了以下帖子:
< a href =http://stackoverflow.com/questions/10435510/storing-something-other-than-a-string-in-suspensionmanager-sessionstate>在SuspensionManager.SessionState中存储字符串以外的内容
SerializationException序列化newtonsoft jobject with DataContractSerializer
提前感谢任何建议。
干杯,
拉里
编辑:
我找到了一篇很棒的文章(最后),有一个有用的例子。我已经调整了它,因此它在终止应用程序时执行序列化,但实际的序列化代码很干净,似乎运行良好。
示例Metro app / WinRT:使用XMLAerializer将对象序列化和解除对使用泛型和异步/等待线程的StorageFile和LocalFolder
嗨劳伦斯,
我认为以下链接可能对您有所帮助
http://stackoverflow.com/questions/4875187/datacontract-serialization-exception-data-合同名称是不可预期的
问候,
Vinoth Kumar J
Hello all,
When attempting to serialize my application data, in a Windows Store App I get the following error:
{"Type 'MyObject' with data contract name '' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer."}
I have already tried to decorate the class in question using the relevant tags, in the following way:
[DataContract]
public class SimpleObject
{
[DataMember]
private string _Name;
[DataMember]
public string Name
{
//set the person name
set { this._Name = value; }
//get the person name
get { return this._Name; }
}
}
As well as this, in the SuspensionManager
class I explicitly added my custom object to the _knownTypes
list inside the SaveAsync()
method in the following way:
_knownTypes.Add(typeof(SimpleObject));
However, I am still getting this error. Does anyone have any suggestions?
While researching this problem I have read the following posts:
Storing something other than a string in SuspensionManager.SessionState
SerializationException serializing newtonsoft jobject with DataContractSerializer
Thanks in advance for any suggestions.
Cheers,
Larry
EDIT:
I have found a great article (finally) with a worked example. I''ve tweaked it so it performs the serialization on termination of the app but the actual serialization code is clean and seems to work well.
Example Metro app /WinRT: Serializing and deseralizing objects using XMLSerializer to StorageFile and LocalFolder using generics and async/await threading
Hi Laurence,
I think the below link might helpful to you
http://stackoverflow.com/questions/4875187/datacontract-serialization-exception-data-contract-name-is-not-expected
Regards,
Vinoth Kumar J
这篇关于在Windows应用商店应用中恢复状态时出现序列化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!