Application Life-cycle Management in Windows 8
After completing this chapter, you will be able to
Understand the application manifest settings.
Use the application manifest to modify application capability and appearance.
Deploy and test an application.
Understand the way Windows 8 manages the different running states of an application.
Respond to launching, activating, suspending, and resuming events.
Use the application data store to save data locally.
In preceding chapters, you saw how Windows 8 provides a new user interface, offers a completely new user experience, and exposes a new set of APIs called Windows Runtime (WinRT) APIs through which you can interact with the operating system. You also developed a simple application in Chapter 3, “My first Windows 8 app.”
This chapter introduces the complete application life cycle in Microsoft Windows 8, from deployment, to launch, to uninstallation. You will start by analyzing the various settings in the application manifest. The application manifest is a file that defines an application’s appearance on the Start screen and informs Windows 8 about which WinRT features the application will use. You will also explore how WinRT manages an application’s life cycle at run time, by launching, suspending, resuming, and terminating the application as needed.
First, a Windows 8 application cannot include an app.config file. This means that—just as in a Microsoft Windows Phone or Windows Presentation Foundation (WPF) Web Browser Application (WBA)—you cannot use the classic .NET configuration mechanism to provide application and system settings. There is no System.Configuration namespace or any equivalent classes in the WinRT APIs. The Windows 8 runtime system executes Windows Store applications in a sandboxed process, similar to a Silverlight or WPF WBA. Users cannot navigate to the file system where the application is installed and change files, because Windows 8 apps are mainly downloaded and installed from the Windows Store.
As in a Windows Phone 7.x project, deployment information and many configuration settings are stored in a manifest file that WinRT calls Package.appxmanifest, which is an XML-formatted file that describes various aspects of the project, as you can see in the following listing, taken from a real Windows Store application.
<?xml version="1.0" encoding="utf-8"?> <Package xmlns="http://schemas.microsoft.com/appx/2010/manifest"> <Identity Name="ea15f786-9bb0-4d64-98b0-d251fa375633" Publisher="CN=Devleap" Version="188.8.131.52" /> <Properties> <DisplayName>Learn with the Animals</DisplayName> <PublisherDisplayName>ThinkAhead</PublisherDisplayName> <Logo>Assets\Store_Logo.png</Logo> </Properties> <Prerequisites> <OSMinVersion>6.2.1</OSMinVersion> <OSMaxVersionTested>6.2.1</OSMaxVersionTested> </Prerequisites> <Resources> <Resource Language="x-generate" /> </Resources> <Applications> <Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="ThinkAhead.Windows8KidsGames.App"> <VisualElements DisplayName="Learn with the Animals" Logo="Assets\logo.png" SmallLogo="Assets\small_logo.png" Description="Learn animal noises, names, guess their noises and names, try to read and try to write their names" ForegroundText="dark" BackgroundColor="#464646"> <DefaultTile ShowName="noLogos" WideLogo="Assets\wide_logo.png" ShortName="Learn with the Animals" /> <SplashScreen Image="Assets\splash_screen.png" BackgroundColor="#b4dfba" /> <InitialRotationPreference> <Rotation Preference="landscape" /> <Rotation Preference="landscapeFlipped" /> </InitialRotationPreference> </VisualElements> </Application> </Applications> </Package>
The first section, called Properties, contains information for the Windows Store, such as the title for the application, the name of the publisher, the official logo, and a brief description.
The last section, called Capabilities, contains a list of all the operating system features the application needs to access on the user’s PC or tablet. When application code requests one of these features, the user receives a direct request to give the application specific permission to use the feature. The user can revoke this permission at any time; your code has to fail gracefully if the user denies the permission to use a capability.
This way of working is similar to a Windows Phone 7.x project, where WMAppManifest.xml tells the operating system the capabilities the application requires to run. You can find more information on application capabilities in Chapter 6, “Windows Runtime APIs.”
Figure 4-1 shows the Manifest Designer that Microsoft Visual Studio 2012 provides to simplify the application definition. To open the designer, simply double-click the Package.appxmanifest file in Solution Explorer. The figure shows the real manifest for one of the authors’ applications, called Learn with the Animals. The Application UI tab lets you choose the display name of the application (the name used for the Start screen), a description of the application, three logos for the application, and so on.
Figure 4-1 The Application UI tab.
The first tab of the Visual Studio Manifest Designer produces the following section in the application manifest:
<VisualElements DisplayName="Save The Planet" Logo="Assets\Logo.png" SmallLogo="Assets\SmallLogo.png" Description="ThinkAhead.SaveThePlanet.Win8.UI" ForegroundText="light" BackgroundColor="#222222" ToastCapable="true"> <LockScreen Notification="badgeAndTileText" BadgeLogo="Assets\BadgeLogo.png" /> <DefaultTile ShowName="allLogos" /> <SplashScreen Image="Assets\SplashScreen.png" BackgroundColor="#000000" /> </VisualElements>
VisualElements, as the name implies, defines the display name for the Windows 8 Start screen, the various logos for the tile (Logo), the small tile (SmallLogo), and the wide tile (WideLogo), as well as the supported rotation and the default one, the badge default logo, and the image for the splash screen.
All the required images referenced by the application package manifest are provided as placeholders by the Visual Studio templates for Windows Store applications and are placed in the Assets folder of the project. The default template uses an image for the application logo that displays on the default application tile (Logo.png), an image for the initial splash screen (SplashScreen.png), a small logo image that is shown in the tile if the application switches its tile from code (SmallLogo.png) and, last but not least, an image used by the Windows Store to represent the application (StoreLogo.png). As you can see in Figure 4-1, you can also provide a wide logo that displays if the user chooses a wide tile for the application from the Start screen.
Figure 4-2 shows the application tile in the Windows 8 Start screen. The tile presents the image described in the application manifest as the WideLogo property and, as you will learn in Chapter 9, “Rethinking the UI for Windows 8 apps,” the application can also modify the tile from code or create a secondary tile.
Figure 4-2 The Learn with the Animals wide tile on the Start screen.