How to activate an app (Windows Store apps using C#/VB/C++ and XAML)

Language: JavaScript and HTML | VB/C#/C++ and XAML
5 out of 12 rated this helpful - Rate this topic

Learn how to define the activation experience for your Windows Store app. The example in this topic overrides the OnLaunched method.

Roadmap: How does this topic relate to others? See:

Instructions

Step 1: Override the launch handler

When an app is activated, for any reason, the system sends the Activated event. For a list of activation types, see the ActivationKind enumeration.

The Windows.UI.Xaml.Application class defines methods you can override to handle the various activation types. Several of the activation types have a specific method that you can override. For the other activation types, override the OnActivated method.

Define the class for your application.



<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             x:Class="AppName.App" >

Override the OnLaunched method. This method is called whenever the user launches the app. The LaunchActivatedEventArgs parameter contains the previous state of your app and the activation arguments.



using System;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;

namespace AppName
{
   public partial class App
   {
      async protected override void OnLaunched(LaunchActivatedEventArgs args)
      {
         EnsurePageCreatedAndActivate();
      }

      // Creates the MainPage if it isn't already created.  Also activates
      // the window so it takes foreground and input focus.
      private MainPage EnsurePageCreatedAndActivate()
      {
         if (Window.Current.Content == null)
         {
             Window.Current.Content = new MainPage();
         }

         Window.Current.Activate();
         return Window.Current.Content as MainPage;
      }
   }
}

Step 2: Restore application data if app was suspended then terminated

When the user switches to your terminated app, the system sends the Activated event, with Kind set to Launch and PreviousExecutionState set to Terminated or ClosedByUser. The app should load its saved application data and refresh its displayed content.



async protected override void OnLaunched(LaunchActivatedEventArgs args)
{
   if (args.PreviousExecutionState == ApplicationExecutionState.Terminated ||
       args.PreviousExecutionState == ApplicationExecutionState.ClosedByUser)
   {
      // TODO: Populate the UI with the previously saved application data
   }
   else
   {
      // TODO: Populate the UI with defaults
   }

   EnsurePageCreatedAndActivate();
}

If the value of PreviousExecutionState is NotRunning, the app failed to save its application data successfully and the app should start over as if it were being initially launched.

Remarks

Note  If your app needs to navigate the top level document for any reason, you must first complete activation before attempting to do the top level navigation. If you attempt a top level navigation before activation completes your app will crash. This ensures that your app and the system are in a consistent state before the JavaScript context is torn down and recreated during the navigation.

Related topics

Tasks
How to suspend an app
How to resume an app
Conceptual
Application lifecycle
Guidelines
Guidelines for app suspend and resume
Reference
Windows.ApplicationModel.Activation
Windows.UI.Xaml.Application

 

 

Build date: 10/26/2012

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.