In this tutorial, I showed the first steps with C # and Avalonia. Today I will show you how to change the formula.
For example, a simple button can be integrated into the application with the following mechanism.
The XAML uses an Avalonia {Binding} to bind the Button’s Content property to the ButtonText property on the MainWindowViewModel.
Let’s see the changes compared to the old tutorial.
The file MainWindow.axaml come with these source code in C#:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <Window xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="Avalonia001.MainWindow" Title="Avalonia001"> <StackPanel Orientation="Vertical" HorizontalAlignment="Center" Width="300"> <TextBlock Text="{Binding Greeting}"></TextBlock> <TextBox Text="{Binding Name}"></TextBox> <Button Content="Say Hi!" Click="OnButtonClicked"></Button> </StackPanel> </Window> |
The StackPanel control is a Panel that lays out its children by stacking them horizontally or vertically. StackPanel is typically used to arrange a small subsection of the UI on a page.
The TextBlock control allows for the display of label-like text in the interface.
The TextBox control is an editable text field where a user can input text.
The Button control is a ContentControl that reacts to pointer presses.
These {Binding Greeting} and {Binding Name} I used for binding between controls and to arbitrary, see this documentation.
The file MainWindow.axaml.cs come with these source code in C#:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | using System; using Avalonia; using Avalonia.Controls; using Avalonia.Interactivity; using Avalonia.Markup.Xaml; using Avalonia001.Model; namespace Avalonia001 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); // this is used to show text this.DataContext = new HelloViewModel() { Greeting = "Type something and press the button!"}; #if DEBUG this.AttachDevTools(); #endif } private void InitializeComponent() { AvaloniaXamlLoader.Load(this); } // when button si clicked then run this public void OnButtonClicked(object sender, RoutedEventArgs args) { var context = this.DataContext as HelloViewModel; context.Greeting = $"Hello {context.Name}"; } } } |
I add a folder named Model in the project and I create this file HelloViewModel.cs in order to send and receive and use the PropertyChanged.
This is a good mechanism because the PropertyChanged method would be executed when the ProperyChanged event triggers.
PropertyChanged is an EventHandler, which is a delegate.
The INotifyPropertyChanged interface and thus implements a PropertyChanged event of type PropertyChangedEventHandler.
This event represents the method, which accepts two arguments:
- the sender of type object – this stores a reference to the object, which raises an event.
- the of type PropertyChangedEventArgs – this is used to pass the name of the source property that was updated.
The Greeting and Name auto property is simple to use it.
Let’s see the source code in C#:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Runtime.CompilerServices; namespace Avalonia001.Model { class HelloViewModel : INotifyPropertyChanged { private string _greeting; private string _name; public string Greeting { get => _greeting; set { if (value != _greeting) { _greeting = value; OnPropertyChanged(); } } } public string Name { get => _name; set { if (value != _name) { _name = value; OnPropertyChanged(); } } } // this take the response from changes and use it public event PropertyChangedEventHandler? PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } } |
The result is this: