Programming with Avalonia
Avalonia lets you develop an application using the XAML markup language and C# (or another .NET language) for code. You generally use XAML markup to implement the appearance of an application while using code to implement its behavior.

Markup

XAML is an XML-based markup language that implements an application's appearance declaratively. You typically use it to create windows and user controls, and to fill them with controls, shapes, and graphics.
The following example uses XAML to implement the appearance of a window that contains a single button:
1
<Window xmlns="https://github.com/avaloniaui"
2
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3
x:Class="AvaloniaApplication1.MainWindow"
4
Title="Window with Button"
5
Width="250" Height="100">
6
​
7
<!-- Add button to window -->
8
<Button Name="button">Click Me!</Button>
9
​
10
</Window>
Copied!
Specifically, this XAML defines a window and a button by using the Window and Button elements, respectively. Each element is configured with attributes, such as the Window element's Title attribute to specify the window's title-bar text. At run time, Avalonia converts the elements and attributes that are defined in markup to instances of Avalonia classes. For example, the Window element is converted to an instance of the Window class whose Title property is the value of the Title attribute.
The following images show the user interface that is defined by the XAML in the previous example running on Windows:
Since XAML is XML-based, the UI that you compose with it is assembled in a hierarchy of nested elements known as an element tree. The element tree provides a logical and intuitive way to create and manage UIs.

Code-behind

The main behavior of an application is to implement the functionality that responds to user interactions, including handling events (for example, clicking a menu, tool bar, or button) and calling business logic and data access logic in response. In Avalonia, this behavior can be implemented in code that is associated with markup. This type of code is known as code-behind. The following example shows the updated markup from the previous example and the code-behind:
XAML
C#
1
<Window xmlns="https://github.com/avaloniaui"
2
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3
x:Class="AvaloniaApplication1.MainWindow"
4
Title="Window with Button"
5
Width="250" Height="100">
6
​
7
<!-- Add button to window -->
8
<Button Name="button" Click="button_Click">Click Me!</Button>
9
​
10
</Window>
Copied!
1
using Avalonia;
2
using Avalonia.Controls;
3
using Avalonia.Interactivity;
4
using Avalonia.Markup.Xaml;
5
​
6
namespace AvaloniaApplication1
7
{
8
public class MainWindow : Window
9
{
10
public MainWindow()
11
{
12
InitializeComponent();
13
}
14
​
15
public void button_Click(object sender, RoutedEventArgs e)
16
{
17
// Change button text when button is clicked.
18
var button = (Button)sender;
19
button.Content = "Hello, Avalonia!";
20
}
21
​
22
private void InitializeComponent()
23
{
24
AvaloniaXamlLoader.Load(this);
25
}
26
}
27
}
Copied!
In this example, the code-behind implements a class that derives from the Window class. The x:Class attribute is used to associate the markup with the code-behind class. InitializeComponent is called from the code-behind class's constructor to merge the UI that is defined in markup with the code-behind class. The combination of x:Class and InitializeComponent ensure that your implementation is correctly initialised whenever it is created. The code-behind class also implements an event handler for the button's Click event. When the button is clicked, the event handler changes the text of the button by setting a property on the Button control.
Last modified 1mo ago
Export as PDF
Copy link