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:
- A first chance exception of type
System.Runtime.Serialization.InvalidDataContractException
occurred in System.Runtime.Serialization.dll
- A first chance exception of type
System.Reflection.TargetInvocationException
occurred in mscorlib.dll
- A first chance exception of type
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?