Startup

This topic provides information about how Universal Windows Platform (UWP) applications start and manage their lifecycle.

Overview

UWP applications are launched and managed by the Windows shell. Understanding the startup process is crucial for correctly initializing your application, handling different launch scenarios, and ensuring a smooth user experience. This section covers the core concepts and APIs related to application startup.

Launch Process

When a user initiates an action that launches a UWP app (e.g., clicking a tile, opening a file associated with the app, responding to a background activation), the Windows shell creates a process for the application and invokes its entry point.

The primary entry point for a UWP app is typically defined in the application's manifest file (`Package.appxmanifest`) and is handled by the main application class, which inherits from Windows.UI.Xaml.Application.

Application Class

The Windows.UI.Xaml.Application class is the central point of a UWP XAML application. It manages the application's lifecycle events, including startup, suspension, and termination.

Key Events

OnLaunched
This event is fired when the application is launched for the first time or resumed from a suspended state. It's the primary place to initialize your UI, navigate to the initial page, and handle launch arguments.
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs e)

Parameters:

e
Event data that includes information about the activation, such as arguments passed to the app.
If the application is being launched due to a previous activation that was interrupted (e.g., by a system-initiated suspension), the e.PreviousExecutionState property can be checked to determine how to resume.
OnActivated
This event is fired when the application is activated for reasons other than a standard launch. This includes activations for file pickers, searches, sharing, or protocol launch activations.
protected override void OnActivated(Microsoft.UI.Xaml.WindowActivatedEventArgs e)

Parameters:

e
Event data containing details about the activation.

Main Page Initialization

Within the OnLaunched event handler, you typically create the main window and navigate to the application's initial page.


// In App.xaml.cs
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
    // Do not repeat app initialization code if the window already has content,
    // just ensure that the window is active.
    if (Window.Current.Content == null)
    {
        // Create a Frame to act as the navigation context and navigate to the first page.
        Frame rootFrame = new Frame();

        rootFrame.NavigationFailed += OnNavigationFailed;

        if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
        {
            // TODO: Load state from previously suspended application
        }

        // Place the frame in the current Window
        Window.Current.Content = rootFrame;
    }

    if (e.PrelaunchActivated == false)
    {
        if (Window.Current.Content is Frame rootFrame)
        {
            rootFrame.Navigate(typeof(MainPage), e.Arguments);
        }
        // Ensure the current window is active
        Window.Current.Activate();
    }
}
                    

Launch Arguments

When an application is launched, it can receive arguments that provide context for the launch. These arguments can be passed through the LaunchActivatedEventArgs.Arguments property.

Common scenarios for launch arguments include:

  • Launching from a protocol activation (e.g., myapp://open?id=123)
  • Launching by opening a specific file type
  • Launching from a background task

You should parse these arguments in your OnLaunched method to perform the appropriate actions, such as navigating to a specific page or displaying specific content.

Prelaunching

Windows can prelaunch your application in the background to improve startup performance when the user eventually launches it. This is indicated by LaunchActivatedEventArgs.PrelaunchActivated being true.

If your application performs significant initialization or resource loading, you should consider how to handle prelaunch to avoid impacting the user's foreground experience. You might defer certain costly operations until the application is actually launched by the user.