Freigeben über


I was joking about the penny, but how about some tombstoning sample code?

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 https://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!

TombstoningExample_withUIExtensions.zip

Comments

  • Anonymous
    March 21, 2011
    Peter, Thanks for the code.  This will help those who manage to find it. The real issue here is that, the code you've provided, will only be used by those that read the blog, follow it on twitter etc. We need to get the code into File > New Windows Phone Project templates for a broader use and for MSFT to support it.  I hope this can be integrated into a future SDK update into the templates shipped with VS & Expression Blend. But all in all great work mate :)

  • Anonymous
    March 24, 2011
    Hi Peter, I like what you've done there. I've done a similar thing myself at tombstonehelper.codeplex.com If we put the two together we could have something great. What's the license situation with your sample? Matt.

  • Anonymous
    March 25, 2011
    Hi Matt, please feel free to lift this code and move it into your codeplex library. The more people that get to use it, the better.

  • Anonymous
    March 30, 2011
    Also check out the tombstone helpers by Rudi Grobler -> www.rudigrobler.net/.../wp7-tombstonehelper

  • Anonymous
    April 02, 2011
    Hi Peter, One thing that is missing, how to "CORRECTLY" tombstone Panorama? I have tried but I have a issue with the TitlePanningLayer alignment, any tips? www.rudigrobler.net/.../panorama-tombstone-caveat Rudi Grobler

  • Anonymous
    April 04, 2011
    Hi Rudi, at the moment there is no way to correctly restore the panorama's position, sorry.

  • Anonymous
    April 06, 2011
    How did the facebook app do it!

  • Anonymous
    April 19, 2011
    Does the 7.1 / emulator survive a tombstone?  Today the 7.0 / emulator has a sort of trick that sort of works sort of sometimes, but sort of doesn't, if you know what I mean.  If this is no longer a valid concern for 7.1 then nevermind. I suppose the "new" (pending) emulator is "new" only if the target is 7.0?  If 7.0 is the target then the "old" emulator and all its warts carry on. (question mark) Re: 7.1 / emulator, does the PROFILER work as intended against the EMULATOR?  Is the PROFILER a needs-real-device-to-work tool?  An answer to one or the other will do swell.

  • Anonymous
    April 21, 2011
    Hi Roy, in the next release of the platform ("Mango") the debugger will support both tombstone and non-tombstone cases for when you switch away from the application. For 7.0 apps, they will continue to have the same behaviour (always tombstoned except for cases like PhotoChooser or a rapid sequence of Start->Back).