If your WP application already uses the Sterling database, use Sterling to generate a tombstone (Tombtsone. Jeremy Likness's blog details how to use Sterling to implement Tombstone in the MVVM architecture. There are several main points:
1. Define a Tombstone data class. The Singletone Serialization mentioned in the Sterling User Guide is used.
public class TombstoneModel
{
public TombstoneModel()
{
State = new Dictionary<string, object>();
}
public Dictionary<string, object> State { get; set; }
public T TryGet<T>(string key, T defaultValue)
{
if (State.ContainsKey(key))
{
return (T)State[key];
}
return defaultValue;
}
}
2. Define an ITombstoneable interface. The Active and Deactive methods are called in the NavigatedTo method of view and the NavigatedFrom method.
Store the Tombstone data class in the Deactive Method to the database, and retrieve the Tombstone data in the Active method to restore the view model status.
3. implement this interface on the view model that requires Tombstone
In many cases, the View Model can work only after the View Loaded. Jeremy uses an anonymous event to handle the event and runs the Active method after Loaded.
public static void ActivatePage(this PhoneApplicationPage phonePage, IViewModel viewModel)
{
RoutedEventHandler loaded = null;
loaded = (o, e) =>
{
((PhoneApplicationPage) o).Loaded -= loaded;
if (viewModel is ITombstoneFriendly)
{
((ITombstoneFriendly) viewModel).Activate();
}
};
phonePage.Loaded += loaded;
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
this.ActivatePage(GlobalManager.GetViewModel<IMainViewModel>());
base.OnNavigatedTo(e);
}
In addition, Sterling currently does not support IList serialization. After being Active, it will find that the items in this set are null. This problem is mentioned in the discussion group and can be solved by using List instead.