Saving State in Windows Phone 7

Nov 27, 2011 at 9:26 PM

Hi,

I've run into another issue when trying to save the state of a page to implement tombstoning.

As per Microsoft's code sample, I am saving the viewmodel into state.

After navigating back to a page from tombstoning, my saved state cannot be deserialized.

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

I ended up downloading the source and updating the ViewModelBase-NonGeneric.cs Viewmodelbase.cs and viewmodeldetailbase.cs to have public constructors.

Any chance of having this updated in the toolkit?

Coordinator
Nov 27, 2011 at 11:25 PM

All those are abstract base classes, which means they cannot be instantiated directly -- that's by design. ;-)

I think what you need to do is use the [KnownType] attribute to inform the data contract serializer of the base class.  See here.

Cheers,

Tony

Nov 28, 2011 at 12:11 AM

Are you suggesting I add the [KnownType] to your abstract class so when it is getting serializer it will know about my type?

 

Coordinator
Nov 28, 2011 at 12:58 AM
Yes, have you given that a try?
Nov 29, 2011 at 6:54 AM

I tried, but couldn't get it to work.  I'm still using the public constructor in abstract classes, but I understand that it's not the best from a design perspective.

For now, it's okay.  I'll revisit if I get a chance later.  Thanks.

May 17, 2012 at 5:51 AM
Edited May 17, 2012 at 5:51 AM

 

toclark wrote:

I tried, but couldn't get it to work.  I'm still using the public constructor in abstract classes, but I understand that it's not the best from a design perspective.

For now, it's okay.  I'll revisit if I get a chance later.  Thanks.

 

 

hi,toclark ,how do you fix this problem?can you share your code ,wait for your reply

May 17, 2012 at 5:53 AM
tonysneed wrote:

All those are abstract base classes, which means they cannot be instantiated directly -- that's by design. ;-)

I think what you need to do is use the [KnownType] attribute to inform the data contract serializer of the base class.  See here.

Cheers,

Tony

hi tony,

 

Recently, I have the same problem ,can u wirte some code sample to help me,wait ...........