Application Application Application Application Class

Definition

Encapsulates the app and its available services.

public : class Application : IApplication, IApplication2, IApplication3, IApplicationOverrides, IApplicationOverrides2
struct winrt::Windows::UI::Xaml::Application : IApplication, IApplication2, IApplication3, IApplicationOverrides, IApplicationOverrides2
public class Application : IApplication, IApplication2, IApplication3, IApplicationOverrides, IApplicationOverrides2
Public Class Application Implements IApplication, IApplication2, IApplication3, IApplicationOverrides, IApplicationOverrides2
<Application />
 
Attributes

Windows 10 requirements

Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Remarks

The Application class encapsulates an app and provides the following services:

  • app entry point, particularly for various activation contracts
  • app lifetime management
  • app-scoped resources
  • unhandled exception detection

The Application object is typically provided in the initial XAML for App.xaml. The default project templates in Visual Studio generate an App class that derives from Application and provides an entry point where you can add initialization code.

The App class associates itself with the corresponding XAML by calling the generated InitializeComponent method in its constructor. You can add additional initialization code to the App constructor, but you will typically only add code to associate handlers to Application events. For other initialization code, you should override one or more initialization methods such as OnLaunched.

The system handles app lifetime by suspending your app whenever the user switches to another app or to the desktop, and resuming your app whenever the user switches back to it. However, the system can also terminate your app while it is suspended in order to free up resources. You should handle the Suspending event to save your app state in case of termination, and override the OnLaunched method to restore your app state. You should handle the Resuming event only if you need to refresh any displayed content that might have changed while the app is suspended. You do not need to restore other app state when the app resumes.

Because of its position in the application model, codegen, and activation sequence, Application has some restrictions on its XAML usage:

  • Other than the xmlns declarations and x:Class, no other attribute can appear on the Application root tag.
  • Don't attempt to change x:Class values that come from the project template App.xaml pages, there are additional dependencies on using that naming scheme that exist in the build actions.
  • Don't wire the Application event handlers in XAML. All event wiring should be done in code (usually in the constructor). Also, you generally use method overrides rather than event syntax for an event (for example, you override OnActivated to respond to that phase of the application life cycle.)
  • The only expected properties on an Application instance in XAML is the set of elements to populate the Application.Resources property, using a XAML property element usage. For more info, see Resources. To provide general error handling for any exceptions that your app code doesn't catch, handle the UnhandledException event.

Note

The Application main file must be named App.xaml.

Constructors

Application() Application() Application() Application()

Initializes a new instance of the Application class.

Properties

Current Current Current Current

Gets the Application object for the current application.

DebugSettings DebugSettings DebugSettings DebugSettings

Gets an object that declares how the app behaves when run in a debug environment.

FocusVisualKind FocusVisualKind FocusVisualKind FocusVisualKind

Gets or sets the type of visual feedback that an app can use to indicate the UI element with focus.

This property is valid only for input devices that can generate key events, such as a keyboard or Xbox controller.

HighContrastAdjustment HighContrastAdjustment HighContrastAdjustment HighContrastAdjustment

Gets or sets a value that indicates whether the framework automatically adjusts visual properties when high contrast themes are enabled.

RequestedTheme RequestedTheme RequestedTheme RequestedTheme

Gets or sets a value that determines the light-dark preference for the overall theme of an app.

RequiresPointerMode RequiresPointerMode RequiresPointerMode RequiresPointerMode

Gets or sets whether a UWP app supports mouse mode, which emulates pointer interaction experiences with non-pointer input devices such as an Xbox gamepad or remote control. (All nested elements inherit this behavior.)

Resources Resources Resources Resources

Gets a collection of application-scoped resources, such as styles, templates, and brushes.

Methods

Exit() Exit() Exit() Exit()

Shuts down the app.

LoadComponent(Object, Uri) LoadComponent(Object, Uri) LoadComponent(Object, Uri) LoadComponent(Object, Uri)

Loads a XAML file that is located at the specified relative location, and converts it to an instance of the object that is specified by the root element of the XAML file.

LoadComponent(Object, Uri, ComponentResourceLocation) LoadComponent(Object, Uri, ComponentResourceLocation) LoadComponent(Object, Uri, ComponentResourceLocation) LoadComponent(Object, Uri, ComponentResourceLocation)

Loads a XAML file that is located at the specified relative location, and converts it to an instance of the object that is specified by the root element of the XAML file.

OnActivated(IActivatedEventArgs) OnActivated(IActivatedEventArgs) OnActivated(IActivatedEventArgs) OnActivated(IActivatedEventArgs)

Invoked when the application is activated by some means other than normal launching.

OnBackgroundActivated(BackgroundActivatedEventArgs) OnBackgroundActivated(BackgroundActivatedEventArgs) OnBackgroundActivated(BackgroundActivatedEventArgs) OnBackgroundActivated(BackgroundActivatedEventArgs)

Invoked when the application is activated in the background.

OnCachedFileUpdaterActivated(CachedFileUpdaterActivatedEventArgs) OnCachedFileUpdaterActivated(CachedFileUpdaterActivatedEventArgs) OnCachedFileUpdaterActivated(CachedFileUpdaterActivatedEventArgs) OnCachedFileUpdaterActivated(CachedFileUpdaterActivatedEventArgs)

Invoked when the application is activated due to an activation contract with ActivationKind as CachedFileUpdater.

OnFileActivated(FileActivatedEventArgs) OnFileActivated(FileActivatedEventArgs) OnFileActivated(FileActivatedEventArgs) OnFileActivated(FileActivatedEventArgs)

Invoked when the application is activated through file-open.

OnFileOpenPickerActivated(FileOpenPickerActivatedEventArgs) OnFileOpenPickerActivated(FileOpenPickerActivatedEventArgs) OnFileOpenPickerActivated(FileOpenPickerActivatedEventArgs) OnFileOpenPickerActivated(FileOpenPickerActivatedEventArgs)

Invoked when the application is activated through file-open dialog association.

OnFileSavePickerActivated(FileSavePickerActivatedEventArgs) OnFileSavePickerActivated(FileSavePickerActivatedEventArgs) OnFileSavePickerActivated(FileSavePickerActivatedEventArgs) OnFileSavePickerActivated(FileSavePickerActivatedEventArgs)

Invoked when the application is activated through file-save dialog association.

OnLaunched(LaunchActivatedEventArgs) OnLaunched(LaunchActivatedEventArgs) OnLaunched(LaunchActivatedEventArgs) OnLaunched(LaunchActivatedEventArgs)

Invoked when the application is launched. Override this method to perform application initialization and to display initial content in the associated Window.

OnSearchActivated(SearchActivatedEventArgs) OnSearchActivated(SearchActivatedEventArgs) OnSearchActivated(SearchActivatedEventArgs) OnSearchActivated(SearchActivatedEventArgs)

Invoked when the application is activated through a search association.

OnShareTargetActivated(ShareTargetActivatedEventArgs) OnShareTargetActivated(ShareTargetActivatedEventArgs) OnShareTargetActivated(ShareTargetActivatedEventArgs) OnShareTargetActivated(ShareTargetActivatedEventArgs)

Invoked when the application is activated through sharing association.

OnWindowCreated(WindowCreatedEventArgs) OnWindowCreated(WindowCreatedEventArgs) OnWindowCreated(WindowCreatedEventArgs) OnWindowCreated(WindowCreatedEventArgs)

Invoked when the application creates a window.

Start(ApplicationInitializationCallback) Start(ApplicationInitializationCallback) Start(ApplicationInitializationCallback) Start(ApplicationInitializationCallback)

Provides the entry point and requests initialization of the application. Use the callback to instantiate the Application class.

Events

EnteredBackground EnteredBackground EnteredBackground EnteredBackground

Occurs when the app moves from the foreground to the background.

LeavingBackground LeavingBackground LeavingBackground LeavingBackground

Occurs when the app moves from the background to the foreground.

Resuming Resuming Resuming Resuming

Occurs when the application transitions from Suspended state to Running state.

Suspending Suspending Suspending Suspending

Occurs when the application transitions to Suspended state from some other state.

UnhandledException UnhandledException UnhandledException UnhandledException

Occurs when an exception can be handled by app code, as forwarded from a native-level Windows Runtime error. Apps can mark the occurrence as handled in event data.

See also