Make sure you’ve completed the Installation guide before starting this tutorial.
Creating Your First Project
Create a New Avalonia Project
Open your terminal and create a new Avalonia MVVM application:This creates a new project with the recommended MVVM (Model-View-ViewModel) architecture.
Understanding the Code
Let’s examine the key components of an Avalonia application.Program.cs - Entry Point
The entry point configures and starts your application:Program.cs
AppBuilder.Configure<App>()- Creates the application builder with your App classUsePlatformDetect()- Automatically detects and configures the platform (Windows, macOS, Linux)UseSkia()- Uses Skia for rendering (cross-platform graphics library)WithInterFont()- Includes the Inter font familyStartWithClassicDesktopLifetime()- Starts the application with desktop window support
App.axaml.cs - Application Class
The Application class manages your app’s lifecycle:App.axaml.cs
Initialize()- Loads XAML resources and stylesOnFrameworkInitializationCompleted()- Called when the framework is readyApplicationLifetime- Manages the application lifecycle (different for desktop, mobile, browser)MainWindow- Sets the initial window to displayDataContext- Connects the view to its ViewModel
MainWindow.axaml - Window Definition
XAML defines your UI structure:MainWindow.axaml
Window- The top-level containerxmlns- XML namespaces for Avalonia controlsx:Class- Links to the code-behind filex:DataType- Enables compiled bindings for better performanceDesign.DataContext- Provides design-time data for the previewer
Building a Simple Interactive App
Let’s add some interactivity to make it more interesting.Update the ViewModel
Open
ViewModels/MainWindowViewModel.cs and add a property and command:ViewModels/MainWindowViewModel.cs
Key Concepts Explained
Data Binding
Data binding connects UI elements to ViewModel properties:Greeting changes in the ViewModel, the TextBlock automatically updates.
Commands
Commands handle user interactions:SayHelloCommand when clicked.
Observable Properties
Using[ObservableProperty] from CommunityToolkit.Mvvm automatically generates change notification code:
Greeting property that notifies the UI when changed.
Layout Controls
Avalonia provides various layout controls:Styling Your App
Add styles to customize appearance:App.axaml
Developer Tools
Enable DevTools for debugging:Program.cs
Next Steps
Build a Complete App
Create a full-featured application with multiple views and real functionality
Learn XAML
Dive deeper into XAML markup and layout systems
Explore Controls
Discover the full range of built-in Avalonia controls
Understanding MVVM
Master the MVVM pattern for clean, maintainable code