XAML Views(Code Behind)


namespace MauiApp1;

public partial class MainPage : ContentPage
{
	int count = 0;

	public MainPage()
	{
		InitializeComponent();
	}

	private void OnCounterClicked(object sender, EventArgs e)
	{
		count++;

		if (count == 1)
			CounterBtn.Text = $"Clicked {count} time";
		else
			CounterBtn.Text = $"Clicked {count} times";

		SemanticScreenReader.Announce(CounterBtn.Text);
	}
}

Let’s look closer some items.

ContentPage : MainPage is inherited from the ContentPage class which in turn is a class that represents a single view.

InitializeComponent(); : An XAML compiler method, that produces named item. Constructor to intialize all the controls which are present in MainPage.xaml page. Links the XAML components with their peers in XAML code-behind.

OnCounterClicked : Copes with Clicked event of the button with the name CounterBtn.

  • object sender: Which panel calibrated this control; where this event originated from.
  • EventArgs e: Contains event data.

SemanticScreenReader.Announce(CounterBtn.Text); : Speaks aloud the new button text through the device’s screen reader.

, ,