What you will learn
- Create a .NET MAUI App project
- Replace the starter UI with a greeting interface
- Handle a button tap using C# code-behind
MauiHello with .NET 10. Use the complete file listings below, and leave other template-generated files unchanged unless the lesson explicitly says otherwise. Run the Android target on Windows.1. Create the project
- Choose File > New > Project, select .NET MAUI App, and click Next.
- Name the project
MauiHello. Select .NET 10 when prompted. - Keep the generated
App.xaml,App.xaml.cs,AppShell.xamlandMauiProgram.csfiles. - Replace the contents of
MainPage.xamlandMainPage.xaml.cswith the examples below, keeping the namespace and class names aligned.
2. Understand the XAML
The ContentPage hosts a VerticalStackLayout, an Entry for the visitor’s name, a Button, and a Label for the output. An x:Name attribute makes a control accessible from the C# file. The Clicked event points to a method with the exact same name in code-behind.
3. Run the app
- Save both files and choose the Android emulator in the toolbar.
- Press F5.
- Enter your name and tap Say hello.
- Try leaving the name blank to confirm the fallback message.
The XAML and C# are shared files. They can be reused when targeting iOS, but an iOS build and test must still take place on a Mac.
4. Why this project is cross-platform
Notice that neither file uses an Android-specific class. The button, entry and label are .NET MAUI controls and the event handler is ordinary C#. This illustrates the shared-code model introduced in Lesson 1.
Complete sample code
Use the exact file names and folder locations shown below. Copy each listing into your MAUI project. The separate source files can also be downloaded individually.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiHello.MainPage"
Title="Hello MAUI">
<VerticalStackLayout Padding="24" Spacing="16">
<Label Text="My First .NET MAUI App"
FontSize="26" FontAttributes="Bold" />
<Label Text="A shared C# and XAML interface"
FontSize="15" TextColor="Gray" />
<Entry x:Name="NameEntry"
Placeholder="Enter your name" />
<Button Text="Say hello" Clicked="OnGreetClicked" />
<Label x:Name="ResultLabel" FontSize="20" />
</VerticalStackLayout>
</ContentPage>namespace MauiHello;
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void OnGreetClicked(object sender, EventArgs e)
{
string name = NameEntry.Text?.Trim() ?? "";
ResultLabel.Text = string.IsNullOrWhiteSpace(name)
? "Hello from .NET MAUI!"
: $"Hello, {name}!";
}
}Common problems and solutions
- XAML XFC errors: Check that x:Class is MauiHello.MainPage and the C# namespace is MauiHello.
- Handler not found: Ensure Clicked="OnGreetClicked" and the method spelling matches exactly.
Practice exercise
Add another label displaying “Built with Visual Studio 2026”, then change the button text to “Greet me”.