How to style MAUI ToolbarItems height and alignment on windows and android

KarstenNedergaardRasmussen-4470 25 Reputation points
2023-05-25T11:59:51.62+00:00

It is possible to style Toolbar with ToolbarHandler but how is ToolbarItems and Tollbar height styled .

As image below show the ToolbarItem is not effected by ToolbarHandler settings on Windows and ToolbarHeight is not changed on Android

Code:

        public static MauiApp CreateMauiApp()
        {
        // https://github.com/dotnet/maui/issues/12063
        ToolbarHandler.Mapper.Add("FixPlacement", (handler, view) =>
        {
#if WINDOWS
            handler.PlatformView.DefaultLabelPosition = Microsoft.UI.Xaml.Controls.CommandBarDefaultLabelPosition.Right;
            handler.PlatformView.VerticalAlignment = Microsoft.UI.Xaml.VerticalAlignment.Top;
            handler.PlatformView.Height = 35;
            handler.PlatformView.Padding = new Microsoft.UI.Xaml.Thickness(0);
            handler.PlatformView.Margin = new Microsoft.UI.Xaml.Thickness(0);
#endif
#if ANDROID
            handler.PlatformView.SetPadding(0, 0, 0, 0);
            handler.PlatformView.TextAlignment = Android.Views.TextAlignment.TextStart;
            handler.PlatformView.SetMinimumHeight(35);
            handler.PlatformView.SetTitleMargin(0, 0, 0, 0);
#endif
        });
...

XAML:

    <Shell.TitleView>
        <Label Text="Test Title View" FontSize="8" HeightRequest="35"  VerticalOptions="Center" Margin="0" Padding="0"  ></Label>
    </Shell.TitleView>

    <Shell.ToolbarItems>
        <ToolbarItem>
            <ToolbarItem.IconImageSource>
                <FontImageSource Glyph="A" Color="BlanchedAlmond" Size="8"/>
            </ToolbarItem.IconImageSource>
        </ToolbarItem>
    </Shell.ToolbarItems>

Windows Screen Dump

Windows Screen Dump

Android Screen Dump

User's image

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,900 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,277 questions
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 35,806 Reputation points Microsoft Vendor
    2023-05-29T06:58:02.31+00:00

    Hello,

    After investigation, it was not feasible to modify the ToolbarItem in the handler.

    The reason is that when using the handler, the ToolbarItem is already in the created state, but according to the ToolbarItem Class document, any changes will be ignored after the ToolbarItem is created.

    Any changes made to the properties of the toolbar item after it has been added will be ignored.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful