xamlmaui

Header and Footer in .NET MAUI


I wanted to do the same as this question, but for .NET MAUI: Same header & footer in all WPF windows

It is same header and footer for all windows.


Solution

  • I found a better way to do it using control templates.

    PageLayout.xaml file:

    <ContentPage ...>
        <ContentPage.Resources>
            <ControlTemplate x:Key="PageLayoutTemplate">
                <Grid RowDefinitions="Auto,*,Auto">
    
                    <!--Header-->
                    <Grid>
                        <!--header content-->
                    </Grid>
    
                    <!--Content-->
                    <ContentPresenter Grid.Row="1"/>
    
                    <!--Footer-->
                    <Grid Grid.Row="2">
                        <!--footer content-->
                    </Grid>
    
                </Grid>
            </ControlTemplate>
        </ContentPage.Resources>
    </ContentPage>
    

    SubView.xaml file:

    <PageLayout ... 
                ControlTemplate="{StaticResource PageLayoutTemplate}">
        <Grid>
            <!--Main content-->
        </Grid>
    </PageLayout>
    

    Why this is better:

    1. No code behind
    2. Binding is easier thanks to template binding
    3. A alternate template can be easily added

    Reference: https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/controltemplate?view=net-maui-7.0