c# - Windows Phone 7: Establishing which page is being activated during the Application_Activated event -
i following general best practice principles of restoring persistent , none persistent state , objects when tombstoned app re-activated. can found in microsoft article
the samples show simple re-activation of main page of app. application has multiple pages (any of tombstoned , therfore re-activated) , each 1 binding different viewmodel object. know how ascertain page going activated can selectivly deserialize , recover correct viewmodel object page.
or best practice restore viewmodels or there design pattern this?
i have implemented simple pattern best described -
- in application's activated , deactivated event, send message subscribing pages.
- the pages subscribe message serialization/deserialization of data.
i using laurent bugnion's excellent mvvmlight library windows phone 7. here sample code illustrating message broadcast -
// ensure application state restored appropriately private void application_activated(object sender, activatedeventargs e) { messenger.default.send(new notificationmessage<appevent>(appevent.activated, string.empty)); } // ensure required application state persisted here. private void application_deactivated(object sender, deactivatedeventargs e) { messenger.default.send(new notificationmessage<appevent>(appevent.deactivated, string.empty)); }
within constructor of viewmodel class, setup subscription notification messages -
// register application event notifications messenger.default.register<notificationmessage<appevent>>(this, n => { switch (n.content) { case appevent.deactivated: // save state here break; case appevent.activate: // restore state here break; } }
i found strategy, data relevant page bound viewmodel saved , restored properly.
hth, indyfromoz
Comments
Post a Comment