| MAUI-CollectionView :👈 | 👉:MAUI-CommunityToolkit-1 |
📐 Layouts in .NET MAUI – BindableLayout |
This page explains what ContentPage, ContentView, and Window are in .NET MAUI, and when to use each. For runnable code examples, open the separate page: maui-examples.htmlMAUI Code Examples.
Definition: A ContentPage represents a single screen in a .NET MAUI application.
OnAppearing and OnDisappearing.When to use:
Definition: A ContentView is a reusable UI component that can be embedded inside a ContentPage or other views.
When to use:
Definition: A Window is a top-level container for your app’s UI. Every MAUI app starts with at least one Window.
When to use:
| Component | Purpose | Typical Use Case |
|---|---|---|
| ContentPage | Full screen UI with navigation | Main screens |
| ContentView | Reusable UI component | Custom controls and modular UI |
| Window | Top-level container for Pages | Multi-window desktop scenarios |
See maui-examples.htmlMAUI Code Examples for XAML and C# code samples.
This page shows minimal XAML and C# examples for ContentPage, ContentView, and Window (including multi-window). Copy snippets into your MAUI project structure as indicated by the file names.
Views/HomePage.xamlViews/HomePage.xaml.cs<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.Views.HomePage"
Title="Home">
<VerticalStackLayout Padding="16" Spacing="12">
<Label Text="Welcome to MAUI" FontSize="24" />
<Button Text="Go to Details"
Clicked="OnGoToDetailsClicked" />
<!-- Reuse a ContentView below (see section 2) -->
<local:InfoCardView x:Name="InfoCard" />
</VerticalStackLayout>
</ContentPage>
using MyApp.Views;
namespace MyApp.Views
{
public partial class HomePage : ContentPage
{
public HomePage()
{
InitializeComponent();
// Example of updating a ContentView property if exposed via BindableProperty
// InfoCard.Title = "Quick Tips";
}
private async void OnGoToDetailsClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new DetailsPage());
}
protected override void OnAppearing()
{
base.OnAppearing();
// Page lifecycle hook
}
protected override void OnDisappearing()
{
base.OnDisappearing();
}
}
}
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.Views.DetailsPage"
Title="Details">
<VerticalStackLayout Padding="16" Spacing="8">
<Label Text="Details Page" FontSize="20" />
<Button Text="Go Back" Clicked="OnBackClicked" />
</VerticalStackLayout>
</ContentPage>
namespace MyApp.Views
{
public partial class DetailsPage : ContentPage
{
public DetailsPage()
{
InitializeComponent();
}
private async void OnBackClicked(object sender, EventArgs e)
{
await Navigation.PopAsync();
}
}
}
Controls/InfoCardView.xamlControls/InfoCardView.xaml.cs<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.Controls.InfoCardView">
<Frame Padding="12" HasShadow="True">
<VerticalStackLayout Spacing="6">
<Label Text="{Binding Source={x:Reference Name=Root}, Path=Title}" FontAttributes="Bold" />
<Label Text="{Binding Source={x:Reference Name=Root}, Path=Description}" />
<Button Text="Action" Clicked="OnActionClicked" />
</VerticalStackLayout>
</Frame>
</ContentView>
namespace MyApp.Controls
{
public partial class InfoCardView : ContentView
{
public static readonly BindableProperty TitleProperty =
BindableProperty.Create(nameof(Title), typeof(string), typeof(InfoCardView), default(string));
public static readonly BindableProperty DescriptionProperty =
BindableProperty.Create(nameof(Description), typeof(string), typeof(InfoCardView), default(string));
public string Title
{
get => (string)GetValue(TitleProperty);
set => SetValue(TitleProperty, value);
}
public string Description
{
get => (string)GetValue(DescriptionProperty);
set => SetValue(DescriptionProperty, value);
}
public InfoCardView()
{
InitializeComponent();
}
private void OnActionClicked(object sender, EventArgs e)
{
// Raise an event or Command for parent pages to handle
}
}
}
Add the namespace and set properties:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:MyApp.Controls"
x:Class="MyApp.Views.UsingInfoCardPage"
Title="Using InfoCard">
<controls:InfoCardView Title="Welcome"
Description="This card is reusable across pages." />
</ContentPage>
In a MAUI app, your App creates the first window via CreateWindow. On desktop,
you can create more windows by instantiating Window with a Page and using Application.Current.OpenWindow.
namespace MyApp
{
public partial class App : Application
{
public App()
{
InitializeComponent();
}
protected override Window CreateWindow(IActivationState activationState)
{
return new Window(new NavigationPage(new Views.HomePage()))
{
Title = "Main Window"
};
}
}
}
private void OnOpenSecondaryWindow(object sender, EventArgs e)
{
var secondaryPage = new Views.SecondaryPage();
var newWindow = new Window(new NavigationPage(secondaryPage))
{
Title = "Secondary Window"
};
Application.Current.OpenWindow(newWindow);
}
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.Views.SecondaryPage"
Title="Tools">
<VerticalStackLayout Padding="16" Spacing="8">
<Label Text="This is a secondary window (desktop)." FontSize="18" />
<Button Text="Close This Window" Clicked="OnCloseClicked" />
</VerticalStackLayout>
</ContentPage>
namespace MyApp.Views
{
public partial class SecondaryPage : ContentPage
{
public SecondaryPage()
{
InitializeComponent();
}
private void OnCloseClicked(object sender, EventArgs e)
{
// Close the hosting window
var window = this.Window;
if (window != null)
{
Application.Current.CloseWindow(window);
}
}
}
}
Window.Created, Activated, Deactivated, and Destroying events for window lifecycle if needed.Back to overview: maui-components-overview.htmlMAUI Components Explained.
| MAUI-CollectionView :👈 | 👉:MAUI-CommunityToolkit-1 |