Search code examples
.netxamlmauititlebar

.NET MAUI - How to place a button in the TitleBar?


In my .NET MAUI project, I want to place a button inside the TitleBar right there:

enter image description here

The TitleBar itself is generated automatically by setting the Title of the ContentPage. Does anyone know how to place a button right there?


Solution

  • As suggested by Jason and Steve, you can use Shell.TitleView to achieve the added buttons. For example, the following XAML shows displaying a button in the navigation bar:

    <Shell.TitleView>
        <StackLayout>
            <Button Text="ADD" Clicked="Button_Clicked" HeightRequest="50" WidthRequest="100" HorizontalOptions="End"></Button>
        </StackLayout>
    </Shell.TitleView>
    

    Or you can use the ToolbarItems as shown below:

    <ContentPage.ToolbarItems>
        <ToolbarItem Text="Add" Clicked="Add_Clicked" />
        <ToolbarItem Text="Save" Clicked="Save_Clicked" />
    </ContentPage.ToolbarItems>
    

    However, it needs a Navigation page to show them up. So, you need to modify MainPage wrapped by a navigation page:

    MainPage = new NavigationPage(new MainPage());