Tombstoning in MVVM

Category: windows phone howto

Question

Kenneth Truyers on Tue, 26 Mar 2013 00:10:27


Hello all,

I recently created a nuget package to handle tombstoning in MVVM through the use of attributes.

You can read about it here: Tombstoning made easy in MVVM

The article explains how to use the package and how it works.


Kenneth Truyers http://www.kenneth-truyers.net


Replies

pumpkinszwan on Sun, 07 Apr 2013 06:29:03


Kenneth, this is excellent help. I'm going to include my VB code adapted from your C# so people using VB can use your helper:

(This code requires Kenneth's helper linked in his post above)

 

 

In App.Xaml.vb, add the following line in the Application_Activated sub:

 TombstoneHelper.Application_Activated(sender, e)

Then in the page(s), e.g. MainPage.Xaml.vb, add:

Imports WindowsPhone.MVVM.Tombstone
and the actual code parts:
Firstly, the code that restores the data objects when resumed from tombstoning:

 

  Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs)
        MyBase.OnNavigatedTo(e)
        TombstoneHelper.page_OnNavigatedTo(Me, e)

Then make sure that you call the relevant code when the page is tombstoned:

   Protected Overrides Sub OnNavigatedFrom(e As NavigationEventArgs)
        MyBase.OnNavigatedFrom(e)
        TombstoneHelper.page_OnNavigatedFrom(Me, e)
    End Sub


And make sure anything you need to have restored is declared as a property like this:

    Private _thiscase As Crime
    <Tombstone> _
    Public Property ThisCase() As Crime
        Get
            Return _thiscase
        End Get
        Set(value As Crime)
            _thiscase = value
            OnPropertyChanged("ThisCase")
        End Set
    End Property

Another note:

For people using another helper in order to pass objects to your page with something like this in your OnNavigatedTo:

ThisCase = NavigationService.GetLastNavigationData()

You must make sure this line is skipped if the tombstoning helper is activated or you will overwrite the value with nothing, which is an issue I ran into. I just do the following:

if ThisCase is nothing then ThisCase = NavigationService.GetLastNavigationData()

After the:

 TombstoneHelper.page_OnNavigatedTo(Me, e)
Therefore if the app was tombstoned the correct object(s) is loaded, otherwise the app can assume it's being navigated to from an earlier page rather than being resumed from tombstoning.

 


I'm a self-taught noob amateur. Please take this into account when responding to my posts or when taking advice from me.