Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

    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.

    So, it sounds like I really shouldn't use TombstoneHelper on this page then. I couldn't find anything in the documentation as to telling TH to ignore a particular item so that I can handle it differently... – Luke G. Nov 6, 2012 at 12:49 nm...I can't read. :) Looks like I can direct TH to save certain types of objects. So, I'll have it save all but my listbox, and I'll see what I can do to serialize that and handle it myself. – Luke G. Nov 6, 2012 at 14:15 So, I used JSON.net to serialize the objects I was using, then I saved to isolated storage. It ended out as a lot less messy of a method of accessing that data anyway. So, no more errors and everything seems to tombstone and restore correctly! Thanks for the help! – Luke G. Nov 6, 2012 at 18:21

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.