Windows 8 Development - Application Startup
10 June 2014
Unlike a web application, which always starts because someone has requested a page, Windows Store applications can be started for a whole host of reasons. It could be that the user has to click on the shortcut/tile to start your app. But, it could also be from the camera sharing a photo with your app, another application (e.g. a web browser) sharing data, or an auto-run triggered by the user inserting a flash drive. There are a whole host of activation kinds that your application can respond to.
As you application loads, WebUIApplication provides you with the activated event. This event can be used for any preprocessing that your application needs to do during the startup process. However, it is the only startup event that WebUIApplication offers. It occurs just after DOMContentLoaded. This means that the markup of a webpage has been parsed, but that not all of the resources may be loaded yet.
The activated event gives you a IActivatedEventArgs object as its arguments, and contains information about the reason for the startup.
The activation event above is fairly simple and its arguments are consistent. It always has the properties:
- kind - See the activation kinds enumeration
- previousExecutionState - See Application Execution State enumeration
- splashScreen - See SplashScreen class
If you plan on handling more than one activation kind and there is a distinct difference in the way you handle the different kinds, I recommend using multiple event handlers. This will make your code cleaner, more testable, and easier to read.
As I mentioned in a previous post, WinJS.Application has events that wrap Windows.UI.WebUI.WebUIApplication. WinJS events are more verbose than their WinRT counterpart during application startup.
To use the WinJS events, at a minimum, you have to include WinJS' base.js and call WinJS.Application.start().
WinJS.Application has not one, but three events that fire during application startup.
- WinJS.Application.onloaded event
- WinJS.Application.onactivated event
- WinJS.Application.onready event
Not only are there more events during activation in the WinJS framework, but the event arguments vary based on the "type" parameter. onactivated has a whopping 12 different possible argument types. If not handled using Clean Code practices, a handler of the WinJS activate event can be extremely sloppy and unmanagable. Based on my experience, I feel that this type of event signature will likely lead the average developer to use a switch statement. I find this to be unsavory on the part of the framework developer because it leads his/her consumers down the wrong path. Switch statements should be used sparingly (if at all) because they break well establed practices such as the Single Responsibility Principle. This makes the code harder to test. It makes the tests more brittle because there are multiple reasons for your handler to change. And, most of all, it makes it harder to read and maintain in the long run. If you need to respond to more than one activation type, I recommend separate event handlers.
While it is true that you would have to do a similar thing using the WinRT API, the WinJS API is just that more complicated because of the variation of they types in the event arguments. Furthermore, since the "type" property is given as a string, you cannot compare it to the Windows.ApplicationModel.Activation.ActivationKind enumerator like you could in the WinRT event handler. Also, during debugging, I have often seen the value activated, which isn't even in the kind enum. I find this inconsistency a bit sloppy.
is licensed under a Creative Commons Attribution 4.0 International License