Skip to main content
The 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

The Application class provides:
  • A global set of DataTemplates for data binding
  • A global set of Styles that apply to all windows
  • Application-wide resource management
  • Theme variant support (light/dark mode)
  • Service registration for the Avalonia framework
  • Application lifetime management

Namespace

Avalonia

Inheritance

AvaloniaObjectApplication

Properties

Current
Application?
Gets the current instance of the Application class. Returns null if no application has been initialized.
var app = Application.Current;
DataContext
object?
Gets or sets the application’s data context. This specifies the default object that will be used for data binding throughout the application.
Application.Current.DataContext = new MainViewModel();
DataTemplates
DataTemplates
Gets the application’s global data templates collection. Data templates define how data objects are displayed in the UI.
app.DataTemplates.Add(new FuncDataTemplate<Person>(
    (person, nameof) => new TextBlock { Text = person.Name }
));
Resources
IResourceDictionary
Gets or sets the application’s global resource dictionary. Resources can include brushes, styles, templates, and other reusable objects.
app.Resources.Add("PrimaryBrush", new SolidColorBrush(Colors.Blue));
Styles
Styles
Gets the application’s global styles collection. These styles apply to all windows in the application.
app.Styles.Add(new Style(x => x.OfType<Button>())
{
    Setters = { new Setter(Button.BackgroundProperty, Brushes.Blue) }
});
ApplicationLifetime
IApplicationLifetime?
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 window
  • ISingleViewApplicationLifetime - Mobile/embedded single-view applications
  • ISingleTopLevelApplicationLifetime - Single top-level window applications
  • IControlledApplicationLifetime - Applications with programmatic lifetime control
Note: Cannot be changed after the application is initialized.
RequestedThemeVariant
ThemeVariant?
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.
app.RequestedThemeVariant = ThemeVariant.Dark;
ActualThemeVariant
ThemeVariant
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.
Name
string?
Gets or sets the application name used for various platform-specific purposes (e.g., app menu on macOS, taskbar on Windows).
app.Name = "My Avalonia App";
PlatformSettings
IPlatformSettings?
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

public virtual void Initialize()
Initializes the application by loading XAML and setting up resources. Override this method in your App class to customize initialization. Example:
public class App : Application
{
    public override void Initialize()
    {
        AvaloniaXamlLoader.Load(this);
    }
}

RegisterServices

public virtual void RegisterServices()
Registers the services needed by Avalonia, including:
  • InputManager for handling input events
  • AccessKeyHandler for keyboard navigation
  • FocusManager for focus management
  • Theme variant detection and management
Note: This is called automatically during application setup. Override to register custom services.

OnFrameworkInitializationCompleted

public virtual void OnFrameworkInitializationCompleted()
Called when the framework initialization is completed. Override this to perform final setup after all services are registered and the application is fully initialized. Example:
public override void OnFrameworkInitializationCompleted()
{
    if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
    {
        desktop.MainWindow = new MainWindow();
    }
    
    base.OnFrameworkInitializationCompleted();
}

TryGetResource

public bool TryGetResource(object key, ThemeVariant? theme, out object? value)
Attempts to retrieve a resource from the application’s resource dictionary.
key
object
required
The resource key to look up.
theme
ThemeVariant?
required
The theme variant context for theme-aware resources.
value
object?
required
When this method returns, contains the resource value if found; otherwise, null.
Returns: true if the resource was found; otherwise, false. Example:
if (app.TryGetResource("PrimaryBrush", ThemeVariant.Light, out var brush))
{
    // Use the brush
}

TryGetFeature

public object? TryGetFeature(Type featureType)
Queries for an optional platform-specific feature.
featureType
Type
required
The type of feature to query for.
Supported features:
  • IPlatformSettings - Platform-specific settings
  • IActivatableApplicationLifetime - Application activation events
Returns: The feature instance if available; otherwise, null. Example:
var settings = app.TryGetFeature(typeof(IPlatformSettings)) as IPlatformSettings;

Events

ResourcesChanged
EventHandler<ResourcesChangedEventArgs>?
Raised when resources in the application’s resource dictionary change.
ActualThemeVariantChanged
EventHandler?
Raised when the actual theme variant changes (e.g., when switching between light and dark mode).
app.ActualThemeVariantChanged += (s, e) =>
{
    Console.WriteLine($"Theme changed to: {app.ActualThemeVariant}");
};

Usage Example

using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;

public class App : Application
{
    public override void Initialize()
    {
        AvaloniaXamlLoader.Load(this);
    }

    public override void OnFrameworkInitializationCompleted()
    {
        if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            desktop.MainWindow = new MainWindow
            {
                DataContext = new MainWindowViewModel(),
            };
        }
        else if (ApplicationLifetime is ISingleViewApplicationLifetime singleView)
        {
            singleView.MainView = new MainView
            {
                DataContext = new MainViewModel()
            };
        }

        base.OnFrameworkInitializationCompleted();
    }
}

See Also

Build docs developers (and LLMs) love