Issue with MVVMLight ViewModelBase public parameterless constructor in inherited base class for WP7 Tombstoning

柔情痞子 提交于 2019-12-31 01:46:10

问题


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

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

This causes the serilization to fail with:

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

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.


回答1:


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.

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.

Cheers, Laurent



来源:https://stackoverflow.com/questions/3976170/issue-with-mvvmlight-viewmodelbase-public-parameterless-constructor-in-inherited

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!