What you will learn
- Read values from Entry, Picker and Switch controls
- Validate input without crashing the app
- Provide feedback through an accessible status label
MauiControls 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 form project
Create the MauiControls .NET MAUI App and replace both MainPage files. Use the same field names shown in XAML; the code-behind refers to them directly.
2. Connect controls to code
The Picker exposes a SelectedItem, the Switch exposes IsToggled, and Entry exposes text. The submit button calls the OnSubmitClicked handler when tapped. Validation runs before the success message is displayed.
3. Try valid and invalid entries
- Tap Submit while all fields are empty: a clear validation message should appear.
- Enter a name but no email; then enter a malformed email.
- Fill in a valid-looking email, choose a course and toggle consent on.
- Tap Submit again to see the confirmation label.
4. Refine the UX
Keep labels descriptive and error messages specific. Try focusing the first invalid field or disabling Submit until all required values have been provided. Keyboard and VoiceOver/TalkBack accessibility should be tested on actual devices before release.
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="MauiControls.MainPage"
Title="Registration">
<ScrollView>
<VerticalStackLayout Padding="24" Spacing="12">
<Label Text="Course Registration"
FontSize="26" FontAttributes="Bold" />
<Label Text="Your name" />
<Entry x:Name="NameEntry" Placeholder="Full name" />
<Label Text="Email" />
<Entry x:Name="EmailEntry" Placeholder="you@example.com"
Keyboard="Email" />
<Label Text="Choose a course" />
<Picker x:Name="CoursePicker" Title="Select a course">
<Picker.Items>
<x:String>C#</x:String>
<x:String>.NET MAUI</x:String>
<x:String>Python</x:String>
</Picker.Items>
</Picker>
<HorizontalStackLayout Spacing="12">
<Switch x:Name="ConsentSwitch" />
<Label Text="I agree to register"
VerticalOptions="Center" />
</HorizontalStackLayout>
<Button Text="Submit" Clicked="OnSubmitClicked" />
<Label x:Name="StatusLabel" FontSize="16" />
</VerticalStackLayout>
</ScrollView>
</ContentPage>namespace MauiControls;
public partial class MainPage : ContentPage
{
public MainPage() => InitializeComponent();
private void OnSubmitClicked(object sender, EventArgs e)
{
string name = NameEntry.Text?.Trim() ?? "";
string email = EmailEntry.Text?.Trim() ?? "";
string course = CoursePicker.SelectedItem?.ToString() ?? "";
if (string.IsNullOrWhiteSpace(name))
{
StatusLabel.Text = "Please enter your name.";
return;
}
if (!email.Contains('@') || !email.Contains('.'))
{
StatusLabel.Text = "Please enter a valid-looking email.";
return;
}
if (string.IsNullOrEmpty(course))
{
StatusLabel.Text = "Please select a course.";
return;
}
if (!ConsentSwitch.IsToggled)
{
StatusLabel.Text = "Please confirm your registration.";
return;
}
StatusLabel.Text = $"Thanks, {name}! You selected {course}.";
}
}Common problems and solutions
- The Picker selection is always null: Select one item before tapping Submit.
- The event does not fire: Check the Clicked method name in XAML and code-behind.
Practice exercise
Add an age field and use int.TryParse to require an age of at least 16.