I ran into an issue where a certain page of my app would not restore from tombstoning. Attempting to reach the app would just result in being put back at the home screen.
Three lines were logged to the console during debugging:
System.Runtime.Serialization.InvalidDataContractException
occurred in System.Runtime.Serialization.dll
System.Reflection.TargetInvocationException
occurred in mscorlib.dll
System.Runtime.Serialization.InvalidDataContractException
occurred in System.Runtime.Serialization.dll
Then, I inspected e.ExceptionObject.Message.ToString()
and saw this error:
"Type 'Newtonsoft.Json.Linq.JToken' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute."
I'm using some JObjects
and JTokens
in the cs code for that page. I'm particularly setting the binding values in a listbox to the values from those JObjects
:
<ListBox x:Name="list" Height="600" HorizontalContentAlignment="Stretch">
<!--SelectionChanged="list_SelectionChanged"-->
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding hline}" FontSize="{StaticResource PhoneFontSizeMedium}" TextWrapping="Wrap" Width="474" />
<TextBlock Text="{Binding body}" Margin="0,0,0,36" FontSize="{StaticResource PhoneFontSizeNormal}" TextWrapping="Wrap" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Then in code:
var deserialized = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Mydata>>(messagearray.ToString().Replace("<br/>", "\n"));
list.ItemsSource = deserialized;
For tombstoning, I'm just doing this:
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
this.SaveState(e); // <- first line
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
this.RestoreState(); // <- second line
}
Is there something I should be doing differently in order to be able to tombstone and survive?
TombstoneHelper uses the Page State object directly and that in turn uses the built in serializer to serialize things. Something in your page uses a JToken which the default serializer (DataContractSerializer) can't handle.
As it looks like you're using MVVM, I'd suggest that you manage the serialization and storage of the view model directly yourself and then in the OnNavigatedTo method rehydrate the viewmodel there.
A full repro would help with a more complete solution.