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
How to use this example: Start from a new .NET MAUI App project called 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.
iOS testing: The shared code is designed for MAUI targets, but no iOS build is claimed. iOS compilation, device testing and App Store release require a Mac/Xcode toolchain.

1. Create the project

  1. Choose File > New > Project, select .NET MAUI App, and click Next.
  2. Name the project MauiHello. Select .NET 10 when prompted.
  3. Keep the generated App.xaml, App.xaml.cs, AppShell.xaml and MauiProgram.cs files.
  4. Replace the contents of MainPage.xaml and MainPage.xaml.cs with 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

  1. Save both files and choose the Android emulator in the toolbar.
  2. Press F5.
  3. Enter your name and tap Say hello.
  4. 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.

MainPage.xaml
<?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>
MainPage.xaml.cs
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”.

Expected result: The Android screen shows the name field and displays a custom greeting when tapped.

Official references