C# – First steps with C# and .NET – part 039.

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#:

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#:

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#:

The result is this:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.