Windows 8 applications (aka Windows Store applications) have 3 different life cycle states they can go through:
Handling both Launching and Suspending is pretty much covered out of the box but what if you need to resume from a suspended state? In most normal scenarios you should not have to do too much here because the built in SuspensionManager (this is generated code, not framework code) will handle putting your application back together in terms of what page your user was on and how the page/screen was populated when it was suspended.
However, if you are using data which is gathered from a remote source such as a web service you may want to refresh your application when it is resuming from suspension. But how exactly can we accomplish this?
Hooking up the Resuming Event
You need to wire up the .Resuming event inside your App.cs class. By default the template only wires the Suspending event as it is used in almost every application.
Handling the Resuming Event
As you can see above it does take some effort to get to your underlying viewmodel in order to make the Resume() call but it is well worth it.
We first need to get the current frame. Once we have the frame we want to check to ensure it is of the correct type in order to further check its underlying data context. Finally we want to check out data context to ensure it is of the correct type. Doing this final check will safe guard us and allow us to only make the resume calls on pages which require it.
Doing something useful in our Resume() call
In the Resume() is where you would need to handle any remote data fetching or updating of existing data.
Defining our IResumable interface
Our above interface is small, but it does the trick. It allows us to identify which pages need to be resumed and enables a way to make the call
As you can see handling the Resuming event in a Windows 8 application is not hard, however getting to your underlying data context does take a bit of work, but this work is well worth it in my opinion.
Till next time,
01-26-2013 6:43 AM