How to serialize and deserialize the class object on deactivation and activation of application in windows phone 7?
I want to save the state of application on deactivation because some how my application crashes when i activate the application after deactivating the application,the value for some object is not saved during tombstoned process during deactivation. So i come up with the solution to serialize the objects on deactivation and deserialize them on activation. I followed the article http://onishimura.com/2010/07/25/windows-phone-7-tutorial-creating-a-simple-notes-app-with-silverlight-part-1/.
On serializing file, it gives an exception InvalidDataContractException showing the message:
"Type 'System.Windows.UIElement' cannot be serialized. Consider marking it with the DataContractAttribute attri开发者_StackOverflow中文版bute, and marking all of its members you want serialized with the DataMemberAttribute attribute."
The class object which i want to save is also have some objects of PhoneApplicationPage and Usercontrols. So how can i fix this?
You can't serialize any arbitrary C# object type, and especially not any Silverlight UI objects - that's by design. You should really only serialize the actual data you rely on in building up the UI, and then retrieve this data as needed when your page is created or navigated to (*).
The linked example doesn't contain any UIElements in its DataContract, just strings which are serializable.
(*) Part 2 of that tutorial actually deals with loading the data in this way.
精彩评论