WP7Contrib – Tombstoning Part II The View Model

In the last post I introduced the idea of separating the tombstoning into two separate responsibilities, the View and the View Model. Each has a different responsibility; the View for specific information to enhance the User Experience and the View Model for databound properties.

Last time it was all about the View so now its time for the View Model to step up and we can hear her story. As we have already hinted, the View Model tombstoning tends to be all about your properties that you have exposed so that you can databind these in Xaml or code behind to visual controls.

The View Model mechanism is slightly more involving as the process is a little more manual as you will have to explicitly define which properties in the View Model you want to be tombstoned. It also has a dependency on the Storage Service interface. We typically wire this in via a couple of methods called IsBeingActivated and IsBeingDeactivated and they live in a base View Model that all the View Models in the app subclass.

    protected override void IsBeingActivated(IStorage storage)
    protected override void IsBeingDeactivated(IStorage storage)

If you’ve checked out some of the samples on the WP7C codeplex site you maybe aware that we tend to create a class called ViewModelBaseWP7 which subclasses the MVVM Light ViewModelBase. For me its the class I copy first when creating a new project as its contains a whole bunch of plumbing that we use in the app, what always comes next is the ViewModelLocator and Bootstrapper classes, anyway if you want to use them awesome if not no worries.

In the sample code you will find a View Model called MainViewModel that all the Views are databound too. The rule here is to add each of these databound properties to the overridden methods and the IStorage interface provides the Read and Write methods. You will notice in the sample code that I am only stashing basic types, you can stash more complex types however you need to ensure that they can be serialised by Silverlight Serializer. Ollie has a great post on how to do this using the IsCachable method.

protected override void IsBeingActivated(IStorage storage)
    var tbv = storage.Read<string>("TextBoxValue");
    var cbv = storage.Read<bool>("CheckBoxValue");
    var pbv = storage.Read<string>("PasswordBoxValue");
    var rbv = storage.Read<bool>("RadioButtonValue");
    var sv = storage.Read<int>("SliderValue");
    var tgbv = storage.Read<bool>("ToggleButtonValue");

        () =>
            this.TextBoxValue = tbv;
            this.CheckBoxValue = cbv;
            this.PasswordBoxValue = pbv;
            this.RadioButtonValue = rbv;
            this.SliderValue = sv;
            this.ToggleButtonValue = tgbv;


protected override void IsBeingDeactivated(IStorage storage)
    storage.Write("TextBoxValue", this.TextBoxValue);
    storage.Write("CheckBoxValue", this.CheckBoxValue);
    storage.Write("PasswordBoxValue", this.PasswordBoxValue);
    storage.Write("RadioButtonValue", this.RadioButtonValue);
    storage.Write("SliderValue", this.SliderValue);
    storage.Write("ToggleButtonValue", this.ToggleButtonValue);

The next thing that we need to do is the plumbing for activating and deactivating the View Models, now depending on what sort of technique you’re using then you have a couple of choices on how you want to do it. We prefer to do this in the View Model Locator where we can find out what is happening to the View Models. In order to sniff out what state our View Models are in we can use the Phone Application Service who can tell us what is happening about the state of the app. Therefore the implementation can be done by adding in a check in the constructor to see if the StartupMode is Activate and hooking up to the Deactivated event. There are numerous ways you can implement the activate and deactivting hooks the approach I have used here is the one we use.

And there you go your done! The View Model will now tombstone all the properties that are supplying data being visualised and when combined with the View state tombstoning we have complete support for tombstoning in the app.

Be Sociable, Share!

Tags: , ,

Leave a Reply