ListView and ScrollViewer Tombstoning helper
This article provides a library which saves the scroll position of ListView and ScrollViewer controls during tombstoning.
Article Metadata
Code Example
Tested with
Compatibility
Article
Introduction
One of the Windows Phone design guidelines is that page states should be saved when a running application is tasked-away from (for example by selecting the Windows button) so that it can be restored to the same state when it is restarted. This gives users the impression that the application is simply being returned to activity, rather than fully restarted.
Apps that are tasked away from are initially made dormant (ie still in memory but not active) and if you switch back to them nothing will have changed. However in low memory an app may be tombstoned (removed from memory). In order to restore a tombstoned application you need to restore it from settings you have previously saved to memory.
This article provides a utility class (StateManager) that handles saving and restoring the scroll bar states of ListView/ScrollViewer controls, and demonstrates its usage.
Problem
Before explaining how the utility class works, let's see what happen when the scroll position is not saved:
- Create a "Windows Phone Databound Application".
- Run the app then scroll the list to the last item.
- Press the Start button (to task away from the application)
- Press the Back button (to return to the application)
The state is preserved, because the application was dormant.
Now, stop debugging the application and go to the properties of the project. In the Debug section, check the option "Tombstone upon deactivation while debugging" to force the app to tombstone if tasked away from when debugging.
Execute the last 3 steps and you’ll see that the list scroll position is not saved.
Solution
To fix this issue, insert the StateManager class (provided at the end of the article) in your project and call StateManager.SaveScrollViewerOffset() and StateManager.RestoreScrollViewer on the list that you want to save/restore scroll position. The best places to insert those calls are in the page methods OnNavigatedTo() and OnNavigatedFrom().
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
StateManager.RestoreScrollViewerOffset(MainListBox);
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
StateManager.SaveScrollViewerOffset(MainListBox);
}
Download the sample project: File:SaveScrollViewerApp.zip



Hamishwillee - Why not use Tombstoning helper for Windows Phone 7?
Hi Sébastien
Looks like a helpful library, but why not use the far more generic Tombstoning helper for Windows Phone 7?
I have also renamed to reflect this supplies a library, not instructions on how to implement things in code.
I have subedited the article to improve the readability and adherence to wiki style. In particular note that the introduction is now more clear - as I understand it the Windows Phone style guidelines reflect only how apps should behave when an app is tasked away from, not how they should behave if exited. In any case, the never version makes the difference between dormant and tombstoning clear.
In terms of wiki style, please link to API reference items the first time they are used, and subsequently use Icode template to mark them up as monotype "code" style. Also please quickly look at the final page to check the layout is OK - you will note I changed the bulleting because it wasn't rendering properly.
Thanks for taking the time to write this article!
regards
Hamishhamishwillee 07:04, 13 September 2012 (EEST)
ArchieCoder - Suggestions
Hello,
If I add more method helpers into my library, I'll definitively rename it to TombstoningHelper which make a lot of sense.
I'll take note about the API reference.
What do you mean by: "Icode template to mark them up as monotype "code" style"?
Thank you
ArchieCoderArchieCoder 15:25, 13 September 2012 (EEST)
Hamishwillee - @ArchieCoder - there is already a tombstoning helper
Hi ArchieCoder
No, my point was that there is already a TombStoning helper library in codeplex, which I've linked to above - so you would need your own name! This covers a lot more controls than yours, so I was wondering first why you rolled out the specific library (probably you did so before this existed) and second if you are aware of this helper, why use yours in preference?
I also changed the title of your article to "helper" because I wanted to make it clear that this article doesn't really describe the code for saving states, but instead provides code that you can use to do so.
It makes it more readable if you highlight inline code items (like class and function names) in the text. So for example when I refer to ListView it is better to mark it up as ListView - ie in monotype font. To do this I use the Icode template like this: {{Icode|ListView}}. Note also that the first time I mention an inline code item I'll tend to link it instead - e.g. ListView
Does that make sense?
Regards
Hamishhamishwillee 04:24, 14 September 2012 (EEST)