Surviving Tombstones

Allowing your ViewModels to survive tombstones is easy, first:

Register the hydrator:

The registration is bound to the ViewModel type when ever this type is asked to be dehyrated, it will use the registered object, and store it against the ApplicationState key.
The ViewModel type must be deserializable using the DataContractSerializer.

        using XamlMvp.Modelling;//or create your own IHydratable!
        ...

        protected override void InitializeHydrators(XamlMvp.IHydratorBinder hydrators)
        {
            hydrators.Add<ViewModel>(new Hydrator<ViewModel>("ViewModelKey"));
        }


DeHydration: On Navigating From Page Presenter Event

Then on NavigatedFrom simply dehyrate the view model when required. Note there is a e.IsTombstone property.

        void View_NavigatedFrom(object sender, XamlMvp.Controls.MvpNavigationEventArgs e)
        {
            Hydrators.DeHydrate(e.State, View.Model);

            /*or*/

            e.State.DeHydrate(View.Model);
        }

ReHydration: During Registration

Upon re-initalization pass the ViewModel in through your Presenters constructor.

        protected override void InitializePresenters(IPresenterBinder presenters)
        {
            presenters.RegisterPage<IViewPage<EasyViewModel>, EasyPresenter>((view, state) => new EasyPresenter(view, state.ReHydrate<EasyViewModel>()));   
        }

Last edited May 23, 2011 at 10:11 AM by hoffmanic, version 7

Comments

No comments yet.