Tombstoning helper for Windows Phone 7
This article explains how to use TombstoneHelper, a small library which makes it easy to persist page data during navigation.
See Also
- Home Page (CodePlex)
- Documentation (CodePlex)
Article Metadata
Tested with
Compatibility
Article
Contents |
Introduction
Windows Phone 7 does not in general allow the execution of background apps. Instead an app that is navigated away from (but not closed) is first moved into a "dormant" state, where no processing occurs, and may later be "tombstoned" (removed from phone memory). Apps are given the opportunity to save their page state when navigated from, and also to restore it when recovered from tombstoning (see the Windows Phone Execution Model for more information).
TombstoneHelper is a small helper which handles most of the work of saving/restoring the app state when tombstoning occurs in Windows Phone 7. TombstoneHelper saves the contents, checked state and scroll positions of TextBoxes, PasswordBoxes, CheckBoxes, RadioButtons, Sliders, ListBox and ScrollViewers that you have named (using the Name attribute).
This article provides an overview of how the helper is used (more can be found in the official documentation), along with an overview of using it with a base page class.
Setup
- Download the TombstoneHelper.dll from CodePlex
- Add a reference to it in your project
Usage
Using TombstoneHelper is simple. All you have to do is add a using statement and override your page's OnNavigatingFrom() and OnNavigatingTo() methods:
using TombstoneHelper;
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
base.OnNavigatingFrom(e);
// Save the state
this.SaveState(e);
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
// Restore the saved state
this.RestoreState();
}
For greater efficiency you can also specify just those components that the page uses. For example:
The API can also be extended to support other UI component types: this is also covered in the API documentation.
Using a base class
I usually create a base class that handles all the saving and then derive all my application pages from that:
Base-class: Handles the save and restore.
namespace Foo.Pages
{
public class FooPageBase : AutoTombstonePage
{
public FooPageBase()
{
}
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
base.OnNavigatingFrom(e);
this.SaveState(e);
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
this.RestoreState();
}
}
}
Page's codebehind: Inherit the base-class
namespace Foo.Pages
{
public partial class ListsPage : FooPageBase
{
public ListsPage()
{
InitializeComponent();
}
}
}
Page's XAML:
<local:FooPageBase x:Class="Foo.Pages.ListsPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:local="clr-namespace:Foo.Pages"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignWidth="480"
d:DesignHeight="696"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait"
Orientation="Portrait"
shell:SystemTray.IsVisible="True"
DataContext="{Binding ListPage, Source={StaticResource Locator}}">


Contents
Hamishwillee - Very useful information indeed!
Hi Juha
Thanks for this article. I have subedited it - please check that I have not "undone" any of the information you wanted to convey.
A few points on the changes I made (FYI):
In general we also try not to just "replicate" information held elsewhere - because normally there isn't much point doing so (aside from the worry of copyright and plagiarism issues). So in a case like this we'd normally just have the minimum to allow users to find the right information. However in this case I've slightly amended the article so it is the "using in a base class" that "value adds" this over the original. An even better "value add" for this would be to show a worked example of adding support for your own type of UI component - since that isn't well covered in the original documentation. Hope that makes sense - its more "for your future thinking)
This is really useful!
Regards Hamish
PS
What devices did you try this on? I like to keep the ArticleMetaData up to date.hamishwillee 05:33, 13 August 2012 (EEST)
Chintandave er - Thanks !
Hi, First Thanks for this useful article.
Still I suggest you to add some images/screenshot of the demo output. So User can easily understand what you are trying to say without reading the each line. If it is not generating any output that can capture then you can draw a sketch which shows the main functionality of this code if possible.
Thanks again,
Chintan Dave.Chintandave er 08:53, 13 August 2012 (EEST)
Jristola - Hi Hamish
No edits are fine. I though that categorizing the article was enough but I can put the platform in the title from now on. This article was just a sideproduct of coding on a hobby project so didn't spend too much time on it yet.jristola 10:09, 13 August 2012 (EEST)
Riussi - Tested on
Lumia 800riussi 10:19, 13 August 2012 (EEST)
Hamishwillee - Hi Jristola/Riussi
Hi Juha
Thanks for getting back to me. I've added the device up in the ArticleMetaData.
I think the article is very useful as it is easy to miss useful third party libraries, and in many cases using a base class is a useful idea. If you have time to add more later, then great.
I was wondering if it is worth creating a wiki article "Useful 3rd Party libraries for Windows Phone" to catch all these. Reason being another one was recently brought to my attention as a comment here: Facebook_authentication_in_Windows_Phone_application. What do you think - better to just use Google or is asking users about what they like a good idea?
Putting the dev framework or platform in the title is highly recommended to remove any ambiguity about the platform for readers. Its not such a big problem in this case because Tombstoning is only associated with WP (although a beginner might not know that and would have had to open it to find out if browsing the Files/Data category (for example) ). However we've had masses of cases in the past where people have used minor variations on topic names like "Getting battery strength" for Symbian C++, Python, Qt, Java ME etc. IMO it is better just to include in the name to make it very clear up front.
Regards
Hamishhamishwillee 07:34, 15 August 2012 (EEST)
Hamishwillee - @Chintandave er - disagree
Hi Chintan
While what you have said would be useful and good advice in 95% of articles we see, I disagree in this case. There isn't any demo output or anything you can usefully show in an image. The use is straightforward and anything more than the code fragments shown would complicate the article, not simplify it.
Regards
Hamishhamishwillee 07:42, 15 August 2012 (EEST)