The Main Window
You can now start your tour of an Avalonia project. We'll start with the main application window. Open the MainWindow.axaml file.
In Avalonia, XAML files have the extension .axaml (and not .xaml). This represents 'Avalonia XAML' and the file extension was introduced for technical reasons.
What is Happening?
In the MainWindow.axaml XAML file, the <Window>...</Window>
XAML tag represents an Avalonia window. Like other
Avalonia controls; the window will be drawn on the target platform with 4 layout zones: margin, border, padding and content.
The MainWindow Content
Inside the Windows Content, you will see a <TextBlock>...</TextBlock>
XAML tag. This represents a TextBlock
control which draws
Text
to the screen. The Text
property of the TextBlock
is bound to the Greeting property of
the MainViewModel class.
<TextBlock Text="{Binding Greeting}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
You can change the text in the file MainWindowViewModel.cs to see the change reflected in the user interface.
namespace GetStartedApp.ViewModels;
public class MainWindowViewModel : ViewModelBase
{
public string Greeting => "Welcome to Avalonia! This is my added text.";
}
For more information about the concept of control layout zones, see here.
The XAML Previewer
If you're using an IDE with one of our IDE Extensions installed, such as Rider, Visual Studio or Visual Studio Code, you can see live changes to your XAML code in the previewer view.
The XAML Previewer create an instance of your app in a special authoring mode, known as design mode. When your app runs in design mode, it can execute special logic that enables coordination with the visual designer. Design.IsDesignMode
- Rider
- Visual Studio
Navigate to the MainWindow.axaml file and click the Split View button at the top right of the editor window.
- Build the project.
Navigate to the MainView.axaml file and click the Split View button at the top of the editor window.
There may be a red exclamation icon (top left) and the message The designer is loading.... This indicates that the project must be built before the preview pane will respond.
- Build the project.
- Scroll the preview pane to the left to view the preview outline and the text displayed in the top left corner.
- Remove the binding
{Binding Greeting}
and change the text<TextBlock Text="my text" />
You will see the new text in the preview pane change as you type. This is an example of the Avalonia design-time preview behaviour that will help you develop user interface presentation accurately and quickly.
- Run the project to see your new text also appear at runtime.
On the next page, you will add a simple Button
to the window.