Thanks to everyone who posted on the previous post about tombstoning in Windows Phone 7. It was actually quite surprising to see the positive comments about tombstoning (both on the blog and privately through e-mail) where developers are saying they appreciate the feature or don’t consider it a huge burden. Of course not everyone thinks that way; tombstoning can be hard to get right if you are porting an existing app from elsewhere and have an app design that doesn’t deal well with places, or if you build a complex app and then try and implement tombstoning as an afterthought, or even if your application state is just flat-out complex (like many games) or is hard to recreate in a hurry.
As to how to do it with the least amount of pain: our general advice is to rely on databinding to bind your UI to a ViewModel, and then serialize your ViewModel into the page state (for transient data) or to Isolated Storage (for permanent data). Of course there are some bugs or “missing features” in our platform that means that not everything works as smoothly as it should. A few examples of things that don’t get saved automatically (or do get saved but are hard to restore):
- Content of text controls if they have focus at the time of deactivation
- Scroll positions of ListBoxes and ScrollViewers
- Position and state of MediaElement
The project attached to this post serves both as an example of how to use the databinding pattern to save state (for those not familiar with it) and also as a vehicle for a handful of extension methods to help cover the cases above. One thing that is not in the sample project is restoring the focused control or text selection state; I’ve seen that done in some places but I don’t think it is necessary to do this in general; none of the built-in experiences do it as far as I can tell unless those that are almost wholly concerned with text entry (like e-mail compose).
One thing – the sample project tries to play a media file from http://localhost – you can find it in the ViewModel.cs file – you can replace this with a file of your own choosing hosted on your local PC, or you could put some content into the App package instead.
I hope you get something out of the attached project, and thanks again for your thoughts. Let me know if there’s something else busted!