WP7 Tombstoning 的继承基类中 MVVMLight ViewModelBase 公共无参数构造函数的问题 [英] Issue with MVVMLight ViewModelBase public parameterless constructor in inherited base class for WP7 Tombstoning

查看:58
本文介绍了WP7 Tombstoning 的继承基类中 MVVMLight ViewModelBase 公共无参数构造函数的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 通过将我的 ViewModel 转储到 PhoneApplicationService 状态来处理 Wp7 中的墓碑(请参阅此 更多信息的链接).

I am handling tombstoning in Wp7 by dumping my ViewModel into the PhoneApplicationService state (see this link for more info).

我的 ViewModel (VM) 继承自 MVVM Light Toolkit (ViewModelBase),它只有一个受保护的无参数构造函数.

My ViewModel (VM) inherits from the MVVM Light Toolkit (ViewModelBase) which has a protected parameterless constructor only.

这会导致序列化失败:

"The type 'GalaSoft.MvvmLight.ViewModelBase' cannot be deserialized in partial trust because it does not have a public parameterless constructor."

请原谅我的无知,但序列化对我来说是新的 - 我想我明白为什么它会失败,但我正在努力想办法解决它.例如,我可以像在类 ([IgnoreDataMember]) 中做某些字段一样将整个基类标记为不可序列化或忽略吗?我不需要存储这个类中的任何东西.

Excuse my ignorance but serialization is new to me - I think I understand why it's failing, but I am trying to think of ways around it. For example, can I mark the entire base class as non-serilizable or ignored like I do certain fields in classes ([IgnoreDataMember])? I don't need to store anything that is in this class.

反正有这个吗?我不想编辑该程序集的源代码以将其标记为公开而不是受保护.

Is there anyway around this? I don't want to edit the source of that assembly to mark it public instead of protected.

推荐答案

StyleCop 不赞成抽象类中的公共默认构造函数,这就是我将 ViewModelBase 设为保护的原因.正如您所发现的,这会导致序列化问题.这个问题在 WP7 中更为严重,为了安全保存,它很容易在序列化中转储整个 vm.

Public default constructors in abstract classes are frowned upon by StyleCop, which is why I made the ViewModelBase one protected. As you found out, this however causes issues with serialization. This issue is more acute in WP7 where it is tempting to dump the whole vm in serialization for safe keeping.

现在,我能提出的唯一解决方法是实现您自己的 viewmodelbase 类.我会考虑在将来的版本中将构造函数更改为 public.

Right now, the only fix I can propose is to implement your own viewmodelbase class. I will consider changing the constructor to public in a future release.

干杯,洛朗

这篇关于WP7 Tombstoning 的继承基类中 MVVMLight ViewModelBase 公共无参数构造函数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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