Application class encapsulates Avalonia application-specific functionality. It inherits from AvaloniaObject and provides the foundation for managing your application’s lifecycle, global resources, and styling.
Overview
TheApplication class provides:
- A global set of
DataTemplatesfor data binding - A global set of
Stylesthat apply to all windows - Application-wide resource management
- Theme variant support (light/dark mode)
- Service registration for the Avalonia framework
- Application lifetime management
Namespace
Inheritance
AvaloniaObject → Application
Properties
Gets the current instance of the Application class. Returns
null if no application has been initialized.Gets or sets the application’s data context. This specifies the default object that will be used for data binding throughout the application.
Gets the application’s global data templates collection. Data templates define how data objects are displayed in the UI.
Gets or sets the application’s global resource dictionary. Resources can include brushes, styles, templates, and other reusable objects.
Gets the application’s global styles collection. These styles apply to all windows in the application.
Gets or sets the application lifetime manager. This controls the application’s execution model and provides access to platform-specific lifetime features.Currently supported lifetimes:
IClassicDesktopStyleApplicationLifetime- Desktop applications with main windowISingleViewApplicationLifetime- Mobile/embedded single-view applicationsISingleTopLevelApplicationLifetime- Single top-level window applicationsIControlledApplicationLifetime- Applications with programmatic lifetime control
Gets or sets the requested theme variant for the application (e.g., light or dark mode). Set to
ThemeVariant.Light, ThemeVariant.Dark, or null for system default.Gets the actual theme variant currently in use. This is a read-only property that reflects the effective theme after considering the requested theme and system settings.
Gets or sets the application name used for various platform-specific purposes (e.g., app menu on macOS, taskbar on Windows).
Gets the global platform-specific settings. Returns
null if the application hasn’t been initialized yet.Note: Prefer using TopLevel.PlatformSettings when available, as specific top levels might have different settings.Methods
Initialize
App class to customize initialization.
Example:
RegisterServices
InputManagerfor handling input eventsAccessKeyHandlerfor keyboard navigationFocusManagerfor focus management- Theme variant detection and management
OnFrameworkInitializationCompleted
TryGetResource
The resource key to look up.
The theme variant context for theme-aware resources.
When this method returns, contains the resource value if found; otherwise,
null.true if the resource was found; otherwise, false.
Example:
TryGetFeature
The type of feature to query for.
IPlatformSettings- Platform-specific settingsIActivatableApplicationLifetime- Application activation events
null.
Example:
Events
Raised when resources in the application’s resource dictionary change.
Raised when the actual theme variant changes (e.g., when switching between light and dark mode).
Usage Example
See Also
- AppBuilder - For configuring and initializing the application
- AvaloniaObject - Base class for objects with property support
- Application Lifetimes