Share via


Application Class

Definition

Represents the current application and its available services.

/// [Windows.Foundation.Metadata.ContractVersion(Microsoft.UI.Xaml.WinUIContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class Application
[Windows.Foundation.Metadata.ContractVersion(typeof(Microsoft.UI.Xaml.WinUIContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public class Application
Public Class Application
<Application />
Inheritance
Object IInspectable Application
Attributes

Remarks

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

  • app entry point
  • 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 a handler for the UnhandledException event or set the RequestedTheme. You should put other initialization code in the OnLaunched method override.

Tip

For more info, see Windows App SDK app lifecycle.

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).
  • 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()

Initializes a new instance of the Application class.

Properties

Current

Gets the Application object for the current application.

DebugSettings

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

DispatcherShutdownMode

Gets or sets a value that specifies whether the DispatcherQueue event loop exits when all XAML windows on a thread are closed.

FocusVisualKind

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

HighContrastAdjustment

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

RequestedTheme

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

Resources

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

Methods

Exit()

Shuts down the app.

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.

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.

OnLaunched(LaunchActivatedEventArgs)

Invoked when the application is launched. Override this method to perform application initialization and to create a new window.

Start(ApplicationInitializationCallback)

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

Events

ResourceManagerRequested

Occurs during startup of a new WinUI thread to let you provide a custom IResourceManager implementation to be used by the framework for resolving resource URIs.

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.

Applies to

See also