Class Application
- Namespace
- Avalonia
- Assembly
- Avalonia.Controls.dll
Encapsulates a Avalonia application.
public class Application : AvaloniaObject, INotifyPropertyChanged, IDataContextProvider, IGlobalDataTemplates, IDataTemplateHost, IGlobalStyles, IStyleHost, IThemeVariantHost, IResourceHost, IResourceNode, IApplicationPlatformEvents, IOptionalFeatureProvider
- Inheritance
-
Application
- Implements
- Inherited Members
- Extension Methods
Remarks
The Application class encapsulates Avalonia application-specific functionality, including:
- A global set of DataTemplates.
- A global set of Styles.
- A FocusManager.
- An Avalonia.Application.InputManager.
- Registers services needed by the rest of Avalonia in the RegisterServices() method.
- Tracks the lifetime of the application.
Constructors
Application()
Creates an instance of the Application class.
public Application()
Fields
ActualThemeVariantProperty
public static readonly StyledProperty<ThemeVariant> ActualThemeVariantProperty
Field Value
DataContextProperty
Defines the DataContext property.
public static readonly StyledProperty<object?> DataContextProperty
Field Value
NameProperty
Defines Name property
public static readonly DirectProperty<Application, string?> NameProperty
Field Value
RequestedThemeVariantProperty
public static readonly StyledProperty<ThemeVariant?> RequestedThemeVariantProperty
Field Value
Properties
ActualThemeVariant
Gets the UI theme that is currently used by the element, which might be different than the RequestedThemeVariantProperty.
public ThemeVariant ActualThemeVariant { get; }
Property Value
- ThemeVariant
If current control is contained in the ThemeVariantScope, TopLevel or Application with non-default RequestedThemeVariant, that value will be returned. Otherwise, current OS theme variant is returned.
ApplicationLifetime
Application lifetime, use it for things like setting the main window and exiting the app from code Currently supported lifetimes are:
public IApplicationLifetime? ApplicationLifetime { get; set; }
Property Value
Current
Gets the current instance of the Application class.
public static Application? Current { get; }
Property Value
- Application
The current instance of the Application class.
DataContext
Gets or sets the Applications's data context.
public object? DataContext { get; set; }
Property Value
Remarks
The data context property specifies the default object that will be used for data binding.
DataTemplates
Gets or sets the application's global data templates.
public DataTemplates DataTemplates { get; }
Property Value
- DataTemplates
The application's global data templates.
Name
Application name to be used for various platform-specific purposes
public string? Name { get; set; }
Property Value
PlatformSettings
Represents a contract for accessing global platform-specific settings.
public IPlatformSettings? PlatformSettings { get; }
Property Value
Remarks
PlatformSettings can be null only if application wasn't initialized yet. TopLevel's PlatformSettings is an equivalent API which should always be preferred over a global one, as specific top levels might have different settings set-up.
RequestedThemeVariant
Gets or sets the UI theme variant that is used by the control (and its child elements) for resource determination. The UI theme you specify with ThemeVariant can override the app-level ThemeVariant.
public ThemeVariant? RequestedThemeVariant { get; set; }
Property Value
Remarks
Setting RequestedThemeVariant to Default will apply parent's actual theme variant on the current scope.
Resources
Gets the application's global resource dictionary.
public IResourceDictionary Resources { get; set; }
Property Value
Styles
Gets the application's global styles.
public Styles Styles { get; }
Property Value
- Styles
The application's global styles.
Remarks
Global styles apply to all windows in the application.
Methods
Initialize()
Initializes the application by loading XAML etc.
public virtual void Initialize()
OnFrameworkInitializationCompleted()
public virtual void OnFrameworkInitializationCompleted()
OnPropertyChanged(AvaloniaPropertyChangedEventArgs)
Called when a avalonia property changes on the object.
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
Parameters
change
AvaloniaPropertyChangedEventArgsThe property change details.
RegisterServices()
Register's the services needed by Avalonia.
public virtual void RegisterServices()
TryGetFeature(Type)
Queries for an optional feature.
public object? TryGetFeature(Type featureType)
Parameters
featureType
TypeFeature type.
Returns
Remarks
Features currently supported by TryGetFeature(Type):
- IPlatformSettings
- IActivatableApplicationLifetime
TryGetResource(object, ThemeVariant?, out object?)
Tries to find a resource within the object.
public bool TryGetResource(object key, ThemeVariant? theme, out object? value)
Parameters
key
objectThe resource key.
theme
ThemeVariantTheme used to select theme dictionary.
value
objectWhen this method returns, contains the value associated with the specified key, if the key is found; otherwise, null.
Returns
- bool
True if the resource if found, otherwise false.
Events
ActualThemeVariantChanged
Raised when the theme variant is changed on the element or an ancestor of the element.
public event EventHandler? ActualThemeVariantChanged
Event Type
ResourcesChanged
Raised when the resources change on the element or an ancestor of the element.
public event EventHandler<ResourcesChangedEventArgs>? ResourcesChanged
Event Type
UrlsOpened
[Obsolete("Use Application.Current.TryGetFeature<IActivatableLifetime>() instead.")]
public event EventHandler<UrlOpenedEventArgs>? UrlsOpened