Creating a new project

Visual Studio

The easiest way to get started with Avalonia from Visual Studio is to install the extension from the Visual Studio Marketplace. Once that is installed, you can create an Avalonia MVVM application:
    Start Visual Studio
    Select "Create a new project" from the start window or File -> New -> Project from the main menu
    Search for "Avalonia" and select "Avalonia MVVM Application"
    Click "Next"
    Enter "Todo" as the Project name
    Click "Create"

.NET Core CLI

First install the Avalonia templates for .NET Core by following the instructions here.
Now you can create the application from the template:
1
dotnet new avalonia.mvvm -o Todo -n Todo
Copied!

Project structure

The newly created project will be pre-filled with a number of files and directories:
1
Todo
2
|- App.axaml
3
|- App.axaml.cs
4
|- Assets
5
| |- avalonia-logo.ico
6
|- Models
7
|- nuget.config
8
|- Program.cs
9
|- Todo.csproj
10
|- ViewLocator.cs
11
|- ViewModels
12
| |- MainWindowViewModel.cs
13
| |- ViewModelBase.cs
14
|- Views
15
| |- MainWindow.axaml
16
| |- MainWindow.axaml.cs
Copied!
You can see there are directories for each of the concepts in the MVVM pattern (models, views and view models) as well as couple of other files and directories:
    The Assets directory holds the binary assets for your application such as icons and bitmaps. Files placed in this directory will automatically be included as resources in the application.
    The Models directory is currently empty, but as the name suggests this is where we'll be putting our models.
    The ViewModels directory is pre-filled with a base class for view models and a view model for the application main window.
    The Views directory just contains the application main window for now.
    The App.axaml file is where XAML styles and templates that will apply to the entire application will be placed.
    The Program.cs file is the entry point for execution of the application. Here you can configure the platform options for Avalonia if necessary.
    The ViewLocator.cs file is used to look up views for view models. This will be explained in more detail later.
.xaml or .axaml?
In this tutorial we will use the .axaml file extension for XAML files which is what will be created by the Visual Studio extension. If you're using the .NET Core CLI, the extension will be .xaml. See the Intro to XAML page for information on why this happens.
Last modified 2mo ago
Export as PDF
Copy link