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

如何修改Shell选中选项卡文本颜色?WinUI平台属性失效求助

解决WinUI平台Shell TabBar文本颜色不生效的问题

你的Shell样式在Android平台正常,但WinUI平台忽略TabBarTitleColor和TabBarUnselectedColor,是因为WinUI对Shell的这些全局属性映射支持不完善,需要通过自定义TabBarItem的视觉状态来实现效果。

解决方案:针对WinUI平台自定义TabBarItem样式

在你的Styles.xaml中添加针对WinUI平台的资源字典,自定义TabBarItem的模板,明确设置选中/未选中状态的文本颜色:

<!-- 放在现有Shell样式之后或者资源字典根节点下 -->
<OnPlatform x:TypeArguments="ResourceDictionary">
    <On Platform="WinUI">
        <ResourceDictionary>
            <Style TargetType="TabBarItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="TabBarItem">
                            <Grid Background="Transparent">
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="CommonStates">
                                        <!-- 未选中状态 -->
                                        <VisualState x:Name="Normal">
                                            <VisualState.Setters>
                                                <Setter TargetName="TitleLabel" Property="TextColor" Value="{StaticResource Gray200}" />
                                            </VisualState.Setters>
                                        </VisualState>
                                        <!-- 选中状态 -->
                                        <VisualState x:Name="Selected">
                                            <VisualState.Setters>
                                                <Setter TargetName="TitleLabel" Property="TextColor" Value="{StaticResource Primary}" />
                                            </VisualState.Setters>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
                                    <Image x:Name="IconImage" Source="{TemplateBinding Icon}" WidthRequest="24" HeightRequest="24" />
                                    <Label x:Name="TitleLabel" Text="{TemplateBinding Title}" FontSize="12" Margin="0,4,0,0" />
                                </StackPanel>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ResourceDictionary>
    </On>
</OnPlatform>

关键说明

  1. 利用OnPlatform确保该样式仅作用于WinUI平台,不影响Android的现有效果。
  2. 通过VisualState分别定义Normal(未选中)和Selected(选中)状态下的文本颜色,完全匹配你原本期望的TabBarUnselectedColor和TabBarTitleColor效果。
  3. 模板里保留了默认的图标和文本布局,你可以根据需求调整FontSize、Margin等属性。
  4. 确认StaticResource中的Primary和Gray200在WinUI平台的资源字典中已正确定义,避免资源引用失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 09:07:39