Thursday, March 24, 2011

Making tombstoning easier [#wp7dev]

Sometimes tombstoning can be tricky.
Often it takes a lot of boilerplate code.

Things just got much easier.

Now all it takes is 2 lines of code:
    protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedFrom(e);

        this.SaveState();  // <- first line
    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        this.RestoreState();  // <- second line
    }
Get all the tombstone helper goodness from http://tombstonehelper.codeplex.com/ Already got some tombstoning support in your app but not handling the scrollviewer scroll position? Just do this:
this.SaveState(typeof(ScrollViewer));
Head on over to codeplex: http://tombstonehelper.codeplex.com/ for more details and please be sure to let me know what you think.

10 comments:

  1. Hello,
    I imported your dll to my project but it doesn't seem to work. It does not restore the textboxes contents after the application is resumed.
    What am I doing wrong?

    ReplyDelete
  2. @Σωτήρης Κ.
    Are you sure you're calling "SaveState()" in "OnNavigatedFrom" and "RestoreState" in "OnNavigatedTo"?
    Also make sure that you have assigned names to the TextBoxes in question. If the textboxes are inside a Pivot, make sure that you're using version 2 of the library.
    If none of that works, if you send me something that reproduces the issue I'll investigate further.

    ReplyDelete
  3. Hello again,
    I have done further reasearch and found out that your dll works fine in my single page application. I also tried it in another pivot project and does not work neither.
    Finaly, I am sure I am using the ver 2 of the library and I call the methods SaveState() and RestoreState from the correct methods in MainPage.xaml.cs

    ReplyDelete
  4. I finaly figured out my problem! If the elements we want to save are in a pivot we must also give the pivot a name in order to be saved.

    ReplyDelete
  5. @Σωτήρης Κ.
    Yep, someone else mentioned the issue where wanting to save something inside a pivot but not save the pivot.

    I'll look into this, or at least add some debug output to help indicate the possible cause.

    ReplyDelete
  6. FrankCap2:29 pm

    Matt thanks for your post.

    I am very new to this stuff and I am having trouble on loading content when application is resumed.

    I have a databound xml reader. MainPage(Panaroma) is connected to DetailsPage: When user clicks on the name in Main Page, She gets the details of this name (Age, Gender, Date of Birth etc.) in the DetailsPage.

    I have two noob questions for you:

    1. Where shall I put this save and restore states. In detailspage or mainpage? I was putting in DetailsPage.cs

    2. Does it work with the listboxes and Scrollviewers.

    ReplyDelete
  7. @FrankCap Good Tombstoning support requires each page taking care of it's own state while deactivated. The deatils page should take care of anything it contains and the main page it's own data.

    2. The library takes care of the scroll offsets of both the listbox and scrollviewer but not the data that is shown. As the data may be gerenarated in any number of ways, be in an unknown format and bound to undetermined properties and may even be virtualized there isn't a generic way of saving the data.

    ReplyDelete
  8. FrankCap10:55 pm

    Matt Thank you very much for your answer.

    If I didn't get it wrong, It should be working well on my application. But I couldnt manage to make it work.

    ReplyDelete
  9. FrankCap9:37 am

    Matt, I figured out my problem thanks to the perfect people in stackoverflow.

    My problem was that I was using TextBlocks to save the data which is not saved automatically by Tombstone Helper.

    Regards

    ReplyDelete
  10. @FrankCap Yep you're wanting to sae state of the underlying model. Currenlty only properties of the controsl are saved, not any underlying models. V3 will include ways to help working with models.

    ReplyDelete

I get a lot of comment spam :( - moderation may take a while.