Windows 8 Development - Life Cycle Events

09 June 2014

In another post, I mentioned that you do not need WinJS at all to write a Windows Store Application. Not only is this true of the user controls, but it is also (mostly) true with regard to the application life cycle.

The JavaScript project templates for Store apps and documentation obfuscate much of this information, but with a little experimentation, you can determine what is going on under the covers. There are actually two sets of event APIs that you can use to interact with the life cycle of your application. The first is the Windows.UI.WebUI.WebUIApplication object. It is part of the underlying WinRT framework. The second is the WinJS.Application object. It is a WinJS specific wrapper around Windows.UI.WebUI.WebUIApplication. There is nothing preventing you from mixing your event handlers between both of the objects.

Windows.UI.WebUI.WebUIApplication

Windows.UI.WebUI.WebUIApplication gives you a minimum set of general events. It covers activation, navigation, suspension, and resumption of the application. If you are relying on something other than WinJS (e.g. BackboneJS) for your presentation layer, then this might be all that you need.

WinJS.Application

If you are using the WinJS library as your GUI, then WinJS.Application is a convenient wrapper around the underlying WinRT library events. It gives you more control over your application life cycle by adding additional events at various points during the duration of the WinJS process. This is especially true at application startup and for GUI related events.

To use the WinJS events, at a minimum, you have to include base.js and call WinJS.Application.start().

1 <!-- This is required for WinJS.Application events -->
2 <script src="//Microsoft.WinJS.2.0/js/base.js"></script>
3 <script type="text/javascript">
4   WinJS.Application.start();
5 </script>