Previous MAUI-Pages MAUI-Navigationpage Next

ContentPage in .NET MAUI

ContentPage in .NET MAUI

In .NET MAUI, a ContentPage is the most commonly used page type β€” essentially a single screen that hosts your UI. It’s the foundation for most app views and is designed to hold one main layout, which in turn contains child controls.

πŸ“Œ What is a ContentPage?

  • A Page that displays a single view hierarchy.
  • Typically used for individual screens like login, settings, or product details.
  • Acts as the container for layouts (StackLayout, Grid, etc.) and controls (Label, Button, Entry, etc.).

🎯 Why Use ContentPage?

  • Simplicity: Ideal for straightforward screens.
  • Flexibility: You can nest layouts and controls to build complex UIs.
  • MVVM-friendly: Works seamlessly with data binding and ViewModels.
  • Navigation: Can be pushed/popped in a NavigationPage or used as part of Shell.

βœ… When to Use

  • For most app screens (home, detail, form, etc.).
  • When you need a self-contained view with its own logic and bindings.
  • As the building block inside navigation structures (NavigationPage, TabbedPage, Shell).

🚫 When Not to Use

  • Don’t use ContentPage for multi-view navigation containers β€” use TabbedPage, FlyoutPage, or Shell instead.
  • Avoid cramming too much UI into one ContentPage; split into multiple pages for clarity.
  • Not suitable for global navigation β€” that’s Shell’s job.

⚑ Best Practices

  • Keep it focused: One clear responsibility per ContentPage.
  • Use layouts wisely: Choose StackLayout, Grid, or FlexLayout depending on UI needs.
  • Bind to ViewModel: Set BindingContext for clean MVVM separation.
  • Reuse components: Extract repeated UI into ContentView and include in ContentPages.
  • Navigation consistency: Use Shell or NavigationPage for structured navigation.

🧩 Example: Simple ContentPage

xml
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyApp.MainPage">

    <StackLayout Padding="20">
        <Label Text="Welcome to MAUI!"
               FontSize="24"
               HorizontalOptions="Center" />
        
        <Entry Placeholder="Enter your name"
               Text="{Binding UserName}" />
        
        <Button Text="Submit"
                Command="{Binding SubmitCommand}" />
    </StackLayout>
</ContentPage>
    
  • Label: Displays static text.
  • Entry: Bound to UserName in the ViewModel.
  • Button: Executes SubmitCommand in the ViewModel.

πŸ† Summary

  • ContentPage = single screen container in MAUI.
  • Best for most app views.
  • Works seamlessly with MVVM and data binding.
  • Keep it clean, focused, and reusable.

🐚 Shell + ContentPage

The Shell is a high-level container that organizes your app’s navigation. It can host ContentPages directly, or group them into tabs and flyouts. This makes navigation simpler and more declarative compared to manually managing NavigationPage stacks.

πŸ“Œ Example: ContentPage inside Shell

AppShell.xaml

xml
<Shell xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
       xmlns:local="clr-namespace:MyApp"
       x:Class="MyApp.AppShell">

    <!-- Flyout Item -->
    <FlyoutItem Title="Home" Icon="home.png">
        <ShellContent ContentTemplate="{DataTemplate local:MainPage}" />
    </FlyoutItem>

    <!-- Tabbed Navigation -->
    <TabBar>
        <ShellContent Title="Dashboard" ContentTemplate="{DataTemplate local:DashboardPage}" />
        <ShellContent Title="Settings" ContentTemplate="{DataTemplate local:SettingsPage}" />
    </TabBar>
</Shell>
    

MainPage.xaml (ContentPage)

xml
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyApp.MainPage">

    <StackLayout Padding="20">
        <Label Text="Welcome to MAUI Shell!"
               FontSize="24"
               HorizontalOptions="Center" />
        <Button Text="Go to Details"
                Command="{Binding NavigateToDetailsCommand}" />
    </StackLayout>
</ContentPage>
    

🎯 How It Works

  • Shell defines the navigation structure (flyouts, tabs, routes).
  • ContentPage defines the UI for each screen.
  • You can navigate between pages using routes:
csharp
await Shell.Current.GoToAsync("//DashboardPage");
    

⚑ Best Practices

  • Use Shell for larger apps β€” it reduces boilerplate navigation code.
  • Keep ContentPages focused β€” one screen, one responsibility.
  • Define routes in Shell for clarity and consistency.
  • Use MVVM binding inside ContentPages for clean separation of UI and logic.

πŸ† Summary

  • ContentPage = screen UI
  • Shell = app navigation + structure
  • Together, they give you a clean, scalable way to build modern MAUI apps.
Back to Index
Previous MAUI-Pages MAUI-Navigationpage Next
*