Monday, 24 June 2013

Suspend/resume/activation using Prism for the Windows Runtime

Previously I extended the sample photo viewing app so that the user can covert a colour photo to greyscale, on the PhotoPage. This used MVVM and commands to bind the Command property of a Checkbox control to a DelegateCommand property on the PhotoPageViewModel. When the control’s command is invoked, the code in the view model is executed.

In this blog post I’ll further extend the app so that page state can be serialized during suspension, and de-serialized when the app reactivates. Specifically, the IsGreyscale property of the PhotoPageViewModel class will be serialized/de-serialized so that when the app reactivates following termination, the state of the Checkbox control will be identical to when the app was suspended. Prism for the Windows Runtime makes performing this task very straight forward.

Implementation

Suspend

To have data survive the suspend/terminate cycle with Prism all you have to do is mark any properties whose data you want to survive termination with the custom [RestorableState] attribute.

[RestorableState]
public bool IsGreyscale { get; set; }

When the the page is navigated away from the data contained in any properties marked with the custom [RestorableState] attribute is serialized to disk. All this work is performed by the Prism.StoreApps library. For details of how Prism undertakes this operation see Handling suspend, resume, and activation in AdventureWorks Shopper.

Resume

When an app resumes from suspension it continues from where it was when it was suspended, as the app is still stored in memory. Therefore, the sample app has no special behaviour associated with resuming from suspension. However, other apps may need to refresh content from network connections, the file system etc. For more information see Developing an end-to-end Windows Store app using C++ and XAML: Hilo.

Activation

Windows may terminate an app after it has been suspended, if the system is low on resources. When a Prism app is reactivated, any properties that are marked with the custom [RestorableState] attribute, such as the IsGreyscale property in the sample app, will have their values restored from disk. All this work is performed by the Prism.StoreApps library. For details of how Prism undertakes this operation see Handling suspend, resume, and activation in AdventureWorks Shopper.

To ensure that a greyscale photo is shown on reactivation, provided that the IsGreyscale property is true, the OnNavigatedTo method in the PhotoPageViewModel class calls the ConvertPhotoToGreyscale method.

if (IsGreyscale)
{
    ConvertPhotoToGreyscale();
}

The overall effect of this is that if the user had the greyscale Checkbox control checked when the app suspended/terminated, the Checkbox control will be checked when the app reactivates, thus ensuring that the photo is displayed as greyscale rather than colour.

Summary

In this blog post I’ve further extended the sample PhotoViewer app so that along with frame state, page state can be serialized during suspension and de-serialized when the app reactivates following termination. This is achieved by marking properties whose values you want to survive termination with the custom [RestorableState] attribute.

The sample app can be downloaded here.

No comments:

Post a Comment