Monday, January 28, 2013

Handling Resuming in your Windows 8 Applications

Handling Resuming in your Windows 8 Applications:
Windows 8 applications (aka Windows Store applications) have 3 different life cycle states they can go through:
  1. Launching
  2. Suspending
  3. Resuming
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
public App()
{
    this.InitializeComponent();
    this.Suspending += OnSuspending;
    this.Resuming += OnResuming;
}

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

private void OnResuming(object sender, object o)
{
    var asFrame = Window.Current.Content as Frame;

    var layoutAwarePageContent = ((LayoutAwarePage) asFrame.Content);

    if (layoutAwarePageContent != null)
    {
        if (layoutAwarePageContent.DataContext is IResumable)
        {
            var asIResumable = ((IResumable) layoutAwarePageContent.DataContext);
            asIResumable.Resume();
        }
    }
}

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

public class DashboardViewModel : BaseViewModel, IResumable
{
    public void Resume()
    {
        // do something useful here like fetching data via a web service
    }
}

In the Resume() is where you would need to handle any remote data fetching or updating of existing data.

Defining our IResumable interface

public interface IResumable
{
    void Resume();
}

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,


DIGITAL JUICE

No comments:

Post a Comment

Thank's!