Architectural Overview
Avalonia’s architecture consists of several key layers:Core Components
Application Class
TheApplication class is the entry point and central coordinator for Avalonia applications. It manages:
- Global Resources: Application-wide resource dictionaries
- Global Styles: Styles that apply to all windows
- Data Templates: Templates for rendering data objects
- Service Registration: Dependency injection and service locator pattern
- Lifecycle Management: Application startup and shutdown
The
Application.Current static property provides global access to the current application instance from anywhere in your code.AppBuilder Pattern
Avalonia uses a fluent builder pattern to configure platform-specific services:Program.cs
Runtime Platform Services
Runtime Platform Services
Initializes core platform services like asset loading, reflection helpers, and runtime utilities.
Windowing Subsystem
Windowing Subsystem
Configures platform-specific windowing (Win32, X11, Cocoa, etc.).
Rendering Subsystem
Rendering Subsystem
Sets up the rendering backend (typically Skia).
Text Shaping
Text Shaping
Configures text rendering and shaping.
Service Locator Pattern
Avalonia usesAvaloniaLocator for dependency injection and service resolution:
Service Registration (from Application.cs)
Component Hierarchy
Object Model
Avalonia’s object model follows this inheritance chain:Key Base Classes
AvaloniaObject
Foundation for the property system. Provides styled and direct properties with change notifications.
StyledElement
Adds styling capabilities, including style application and resource lookups.
Visual
Manages the visual tree, hit testing, transformations, and rendering.
Layoutable
Implements the layout system with measure and arrange passes.
Platform Abstraction
Avalonia abstracts platform-specific functionality through interfaces:- IPlatformSettings: Platform-specific settings (theme, accent colors)
- IWindowingPlatform: Window creation and management
- IRenderTarget: Rendering surface abstraction
- IClipboard: Clipboard operations
- IStorageProvider: File system access
Accessing Platform Services
Threading Model
The framework usesDispatcher for thread marshalling:
Thread Marshalling
Feature Discovery
Avalonia provides optional feature discovery throughIOptionalFeatureProvider:
Feature Detection
IPlatformSettings- Platform-specific settingsIActivatableLifetime- Application activation events
Best Practices
Initialize in order
Follow the proper initialization sequence: configure AppBuilder → register services → initialize application → set up lifetime.
Use the locator sparingly
Prefer dependency injection over service locator pattern when possible. Use
AvaloniaLocator for framework-level services.Respect the threading model
Always update UI from the UI thread. Use
Dispatcher.UIThread for marshalling from background threads.Related Topics
- Application Lifecycle - Managing application startup and shutdown
- XAML Basics - Understanding XAML and markup
- Data Binding - Connecting UI to data
- Styling and Theming - Customizing application appearance