Steps to Activate Windows Store App Using JavaScript and C#

USING JAVA SCRIPT

When an app is launched, activation and loading events are fired in a specific order but you can't detect the activation type in the DOMContentLoaded event, so its event handler should not perform tasks related to activation and mostly performs one-time tasks such as registering to handle other events and to set up the UI to replace the splash screen and it should not include code that depends on the saved state.
Order of event firing:

  • DOMContentLoaded: Raised by the System only when a page is refreshed.

  • Activated: Raised by the System only when a running app is activated.

  • window.load: Raised by the System only when a page is refreshed.

Step 1: Register for the DOMContentLoaded event.

Registeration of this event occurs when all Document Object Model (DOM) and script content has been loaded, but not necessarily the images and content. It is recommended to set up any custom loading UI for the app in domcontentloadedHandler.

Example:

document.addEventListener(
"DOMContentLoaded", domcontentloadedHandler, false);

Step 2: Register for the activated event.

Register for the activated event in the global scope. This example sets activatedHandler as the activation handler and this enables you to retrieve the previous state of your app and the activation arguments.

Example:

var
app = WinJS.Application;
app.addEventListener(
"activated", activatedHandler, false);
app.start();

Step 3: Restore application data when an app is suspended then terminated.

The Activated event is sent by the system when the user switches to your terminated app. In the Activated event, check the sessionState object to see whether your session state variable is defined. If defined then the app should load its saved application data and refresh its displayed content.

Example:

function activatedHandler(eventArgs)
{
   
if (eventArgs.detail.kind == Windows.ApplicationModel.Activation.ActivationKind.launch)
    {
       
// Check whether my session state variables are valid.
       
// If so, retrieve the application data saved in the checkpoint handler
       
if (app.sessionState)
        {
           
// TODO: Populate the UI with the previously saved application data
        }
       
else
        {
           
// TODO: Populate the UI with defaults
        }
       
// Initialize the WinJS controls
        WinJS.UI.processAll();
    }
}

Step 4: Retrieve the launch arguments.

When the system launches your app, there may be additional context for the activation. Check the WebUILaunchActivatedEventArgs.arguments property as follows.

Example:

function activatedHandler(eventArgs)
{
   
if (eventArgs.detail.kind == Windows.ApplicationModel.Activation.ActivationKind.launch)
    {
       
if (eventArgs.detail.arguments !== '')
        {
           
// TODO: Parse the arguments string
        }
    }

}

USING C#

Step 1: Override the launch handler.

When an app is started, the system sends an Activated event. The Application.OnActivated method is invoked when the app is activated by some means other than normal launching.

Define the class for your application:


Example:

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

The Application.OnLaunched method is called whenever the user launches the app; override this method to perform application initialization and to display initial content in the associated Window.

Example:


using
System;
using
Windows.ApplicationModel;
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 the 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.

  • CoreApplicationView.Activated event: This event occurs when the view is activated.

    Example:
    publicevent TypedEventHandler<CoreApplicationView, IActivatedEventArgs> Activated 

  • IActivatedEventArgs.Kind property: This property gets the reason that this app is being activated.

         Example:
         
ActivationKind Kind { get; }

  • IActivatedEventArgs.PreviousExecutionState property: This property gets the execution state of the app before this activation, but the app uses this information to determine whether it should restore the saved state.

         Example:
         ApplicationExecutionState PreviousExecutionState {
get; }

Example:
 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.
 

Source: MSDN (http://msdn.microsoft.com/en-us/library/windows/apps/Hh465102%28v=win.10%29.aspx)


Similar Articles