ContentView is a .NET MAUI view for displaying one content object and combining existing controls into a reusable component. It provides the visual basis for custom or templated controls, reducing repeated UI work. section 11.1
Use ContentView when a group of controls represents one repeatable piece of interaction:
- Bundle an Entry with two Buttons to create a touch-friendly numeric stepper. section 11.1
- Define the layout in XAML, then handle control events in code-behind. section 11.1.1
- Expose a value property and synchronize it with typed Entry text or button changes. section 11.1.1
- Reference the custom control from a page through an XML namespace. section 11.1.1
MildredStepper
Create a ContentView containing a three-column Grid: a minus Button, a centered Entry, and a plus Button. Change the Value property when buttons are clicked or when entered text parses as an integer, then place the control on the input page. section 11.1.1
Listing 11.1 The custom stepper layout
<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiStockTake.UI.Controls.MildredStepper">
<Grid ColumnDefinitions="50,120,50">
<Button Grid.Column="0"
Text="-"
Clicked="MinusButton_Clicked"
x:Name="MinusButton"
VerticalOptions="Center"
HorizontalOptions="Center"
MinimumWidthRequest="50"/>
<Entry Grid.Column="1"
x:Name="ValueEntry"
FontSize="42"
HorizontalTextAlignment="Center"
TextChanged="ValueEntry_TextChanged"
VerticalOptions="Center"
HorizontalOptions="Center"/>
<Button Grid.Column="2"
Text="+"
Clicked="PlusButton_Clicked"
x:Name="PlusButton"
VerticalOptions="Center"
HorizontalOptions="Center"
MinimumWidthRequest="50"/>
</Grid>
</ContentView>
The XAML listing shows the ContentView declaration, three-control layout, event wiring, and sizing choices for the custom stepper. section 11.1.1
