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

.NET7 MAUI FlyoutItem样式问题:背景及选中状态颜色修改

解决Shell侧边栏三个问题的方案

针对你遇到的三个问题,以下是具体的修复步骤和代码修改:

问题1:Flyout背景色设置无效

Shell中设置侧边栏背景的正确属性是FlyoutBackgroundColor,而非你使用的FlyoutBackground,修改Shell标签的属性即可:

<Shell FlyoutWidth="90" FlyoutBehavior="{OnIdiom Phone=Disabled, Default=Locked}" 
       FlyoutBackgroundColor="#111111">

问题2&3:选中项时图标和文本颜色不变化

核心问题在于你通过Image绑定FlyoutIcon无法动态修改颜色,且硬编码了Label的TextColor,同时VisualState的Setter配置有误。改用FontImage控件+DataTrigger绑定选中状态的方式解决:

1. 修改FlyoutItem的Icon配置

简化FlyoutItem的Icon定义,只保留FontImageSource的核心属性:

<!-- Desktop/Tablet-->
<FlyoutItem Title="Home">
  <FlyoutItem.Icon>
    <FontImageSource FontFamily="FontAwesomeSolid" Glyph="{x:Static helpers:FontAwesomeIcons.User}" />
  </FlyoutItem.Icon>
  <ShellContent Title="Page1" Route="Page1" ContentTemplate="{DataTemplate page:Page1}" />
</FlyoutItem>
<FlyoutItem Title="Settings">
  <FlyoutItem.Icon>
    <FontImageSource FontFamily="FontAwesomeSolid" Glyph="{x:Static helpers:FontAwesomeIcons.User}" />
  </FlyoutItem.Icon>
  <ShellContent Title="Page2" Route="Page2" ContentTemplate="{DataTemplate page:Page2}" />
</FlyoutItem>

2. 重写Shell.ItemTemplate

替换Image为FontImage,并通过DataTrigger绑定IsSelected属性动态切换颜色:

<Shell.ItemTemplate>
  <DataTemplate>
    <Grid>
      <Grid.RowDefinitions>
        <RowDefinition Height="50" />
        <RowDefinition Height="25" />
      </Grid.RowDefinitions>
      <!-- 使用FontImage直接控制颜色 -->
      <FontImage Grid.Row="0"
                 FontFamily="FontAwesomeSolid"
                 Glyph="{Binding Icon.Glyph}"
                 Size="50"
                 Margin="5,0,5,0"
                 HorizontalOptions="CenterAndExpand">
        <FontImage.Triggers>
          <DataTrigger TargetType="FontImage"
                       Binding="{Binding IsSelected}"
                       Value="True">
            <Setter Property="Color" Value="Red" />
          </DataTrigger>
          <DataTrigger TargetType="FontImage"
                       Binding="{Binding IsSelected}"
                       Value="False">
            <Setter Property="Color" Value="White" />
          </DataTrigger>
        </FontImage.Triggers>
      </FontImage>
      <!-- Label通过DataTrigger切换文本颜色 -->
      <Label Grid.Row="1"
             Text="{Binding Title}"
             FontSize="Body"
             Padding="7,0,7,0"
             VerticalTextAlignment="Center"
             HorizontalTextAlignment="Center">
        <Label.Triggers>
          <DataTrigger TargetType="Label"
                       Binding="{Binding IsSelected}"
                       Value="True">
            <Setter Property="TextColor" Value="Red" />
          </DataTrigger>
          <DataTrigger TargetType="Label"
                       Binding="{Binding IsSelected}"
                       Value="False">
            <Setter Property="TextColor" Value="White" />
          </DataTrigger>
        </Label.Triggers>
      </Label>
    </Grid>
  </DataTemplate>
</Shell.ItemTemplate>

3. 移除无效的FloutItemStyle

原来针对Grid的Style中,修改Image.Source为"Red"是错误的操作(Source是图片资源而非颜色),且VisualState的配置无法正确触发,直接移除该Style即可。

完成以上修改后,侧边栏背景会变为#111111,选中项的图标和文本会切换为红色,未选中时保持白色,符合你的期望效果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 19:05:26