Search code examples
.netxamlmauimaui-windows

How to set an icon image in Menu bar on windows in .MAUI by Xaml?


How can I add An Icon or an Image in menu bar in .MAUI by Xaml here :
enter image description here

Here what I tried ,in Appshell :

<Shell
    x:Class="MauiApp5.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:MauiApp5"
    Shell.FlyoutBehavior="Flyout"
    IconImageSource="dotnet_bot.png"
    FlyoutIcon="dotnet_bot.png"
     
    />
   

In MainPage.xaml :

<MenuBar>
  <MenuItem Text="File">
    <MenuItem Text="Open" Icon="dotnet_bot.png" />
  
</MenuBar>

If My question not clear enough, you could ask for clarification and I will edit it. thanks in advance.


Solution

  • You can use Shell.TitleView to set the icon in the navigation bar. For example, the following XAML(Appshell.xaml) shows displaying an icon:

        <Shell.TitleView> 
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
    
                <Image Grid.Column="0"
                       Source="dotnet_bot.png" 
                       HeightRequest="50"/>
            </Grid>
        </Shell.TitleView>
    
    

    Output:

    enter image description here

    For more details, you can refer to Display views in the navigation bar.