You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为FlyoutItem添加图标?多种尝试均未成功

解决方案

一、为FlyoutItem添加侧边栏图标

你之前的写法存在拼写错误(FlyoutItem.com应为FlyoutItem.Icon),以下是两种正确的实现方式:

1. 使用本地图片(UriImageSource)

确保图片已放入项目的Resources/Images文件夹,且生成操作设置为MauiImage:

<FlyoutItem Title="Products" Style="{StaticResource ProductFlyout}">
    <FlyoutItem.Icon>
        <UriImageSource Uri="product_icon.png" />
    </FlyoutItem.Icon>
    <ShellContent Title="Product List" ContentTemplate="{DataTemplate page:ProductsListPage}" />
</FlyoutItem>

2. 使用字体图标(FontImageSource)

若使用Material Icons等字体图标,先在App.xaml中注册字体:

<FontFamily x:Key="MaterialIcons">MaterialIcons-Regular.ttf#Material Icons</FontFamily>

再在FlyoutItem中配置:

<FlyoutItem Title="Products" Style="{StaticResource ProductFlyout}">
    <FlyoutItem.Icon>
        <FontImageSource
            FontFamily="{StaticResource MaterialIcons}"
            Glyph="&#xe8cc;"
            Color="#333333" />
    </FlyoutItem.Icon>
    <ShellContent Title="Product List" ContentTemplate="{DataTemplate page:ProductsListPage}" />
</FlyoutItem>

二、在顶部标题栏旁显示图标

默认Shell标题栏仅显示文字,要添加图标需通过Shell.TitleView自定义导航栏布局,推荐在对应页面中设置:

打开ProductsListPage.xaml,在页面根元素内添加如下代码:

<Shell.TitleView>
    <HorizontalStackLayout Spacing="8" VerticalOptions="Center" Margin="16,0">
        <Image Source="product_icon.png" WidthRequest="24" HeightRequest="24" />
        <Label Text="Product List" FontSize="18" FontAttributes="Bold" />
    </HorizontalStackLayout>
</Shell.TitleView>

这样就能在标题左侧显示图标,和文字横向排列。

注意事项

  • 图片路径需与实际资源位置一致,本地图片务必设置正确的生成操作;
  • 字体图标需确保字体文件已导入项目,且FontFamily名称配置正确;
  • Shell.TitleView会完全替换默认标题栏,需自行调整布局的间距、对齐方式等样式。

内容的提问来源于stack exchange,提问作者Adam Gong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 06:03:28