'How do I add a toolbar item .net maui?

As the question states I am trying to add a toolbar item/button to shell for adding a database item. Normally in Xamarin forms I was able to add a toolbar item with

<ContentPage.ToolbarItems>
    <ToolbarItem Text="Add"
                 Clicked="AddItem_Clicked"/>
</ContentPage.ToolbarItems>

But I have not figured out how to make this work in .net maui, does anyone have any insight on this? So far I have a shell with flyout menu but I would like to add an "Add" button to the top right corner.

If there was a way to do this on the Shell menu that would be more preferred than the page level but either would be great.

Edit: I was able to get it working with help from the first comment below but I am unable to make adjustments to button width or have it in a stacklayout, so code looks like:

    <Shell.TitleView>
        <Button Text="+" Clicked="AddItem_Clicked" BackgroundColor="LightBlue" MaximumWidthRequest="20" WidthRequest="20"></Button>
    </Shell.TitleView>

Adding a stacklayout around the button makes it not show up anymore. Also this does not work on Windows build as nothing shows up.



Solution 1:[1]

You can use Shell.TitleView to achieve the above page add function.

Here is the xaml code:

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

For more information, please refer to:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/shell/pages#display-views-in-the-navigation-bar

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wen xu Li - MSFT