.NET MAUI中如何修改工具栏图标、汉堡菜单及返回按钮颜色?
.NET MAUI 多平台工具栏图标颜色修改方案
通用配置优化
你当前的NavigationPage样式中已设置IconColor="White",但部分平台未生效,可调整为主题绑定确保深浅模式适配:
<Style TargetType="NavigationPage" ApplyToDerivedTypes="True"> <Setter Property="BackgroundColor" Value="{AppThemeBinding Light={StaticResource DarkGreenBackgroundColor}, Dark={StaticResource Background_Dark}}" /> <Setter Property="BarBackgroundColor" Value="{AppThemeBinding Light={StaticResource DarkGreenBackgroundColor}, Dark={StaticResource Background_Dark}}" /> <Setter Property="BarTextColor" Value="White" /> <!-- 改为主题绑定,适配深浅模式 --> <Setter Property="IconColor" Value="{AppThemeBinding Light=White, Dark=White}" /> </Style>
注:部分平台对IconColor支持有限,需结合平台特定处理。
Windows平台处理
深色模式图标变黑/浅色模式更多图标改白
Windows平台工具栏图标默认继承系统主题色,需通过平台特定资源字典覆盖:
- 在
Platforms/Windows/Resources/Styles/Styles.xaml中添加样式:
<!-- 处理汉堡按钮、更多图标 --> <Style TargetType="NavigationView"> <Setter Property="Foreground" Value="White" /> <Setter Property="PaneToggleButtonStyle"> <Setter.Value> <Style TargetType="ToggleButton"> <Setter Property="Foreground" Value="White" /> </Style> </Setter.Value> </Setter> <Setter Property="OverflowButtonStyle"> <Setter.Value> <Style TargetType="Button"> <Setter Property="Foreground" Value="White" /> </Style> </Setter.Value> </Setter> </Style> <!-- 处理返回按钮 --> <Style TargetType="NavigationBackButton"> <Setter Property="Foreground" Value="White" /> </Style>
- 如需区分深浅模式,在Windows的
App.xaml中添加主题触发器:
<Application.Resources> <ResourceDictionary> <ResourceDictionary.ThemeDictionaries> <ResourceDictionary x:Key="Dark"> <Style TargetType="NavigationView"> <Setter Property="Foreground" Value="White" /> </Style> </ResourceDictionary> <ResourceDictionary x:Key="Light"> <Style TargetType="NavigationView"> <Setter Property="Foreground" Value="White" /> </Style> </ResourceDictionary> </ResourceDictionary.ThemeDictionaries> </ResourceDictionary> </Application.Resources>
Android平台处理
深色模式下更多图标变黑问题
Android溢出菜单图标颜色由toolbarPopupTheme控制,需修改平台特定样式:
- 在
Platforms/Android/Resources/values-night/styles.xml(深色模式样式文件)中添加:
<?xml version="1.0" encoding="utf-8" ?> <resources> <style name="MainTheme" parent="MainTheme.Base"> <item name="android:toolbarPopupTheme">@style/PopupMenuDark</item> </style> <style name="PopupMenuDark" parent="Widget.Material3.PopupMenu"> <item name="android:textColor">@android:color/white</item> <item name="android:iconTint">@android:color/white</item> </style> </resources>
- 统一工具栏图标颜色,在
Platforms/Android/Resources/values/styles.xml中设置:
<style name="MainTheme.Base" parent="Theme.Material3.Light.NoActionBar"> <item name="colorPrimary">@color/dark_green</item> <item name="colorPrimaryDark">@color/dark_green</item> <item name="toolbarIconTint">@android:color/white</item> </style>
同时在Resources/values/colors.xml定义颜色:
<color name="dark_green">#187D21</color>
iOS平台处理
iOS平台工具栏图标颜色可通过两种方式设置:
方式1:MAUI主题绑定
确保NavigationPage的IconColor配置正确,iOS通常会响应此属性:
<Setter Property="IconColor" Value="{AppThemeBinding Light=White, Dark=White}" />
方式2:平台特定代码
在Platforms/iOS/AppDelegate.cs的FinishedLaunching方法中添加:
using UIKit; public override bool FinishedLaunching(UIApplication app, NSDictionary options) { // 设置导航栏所有图标颜色 UINavigationBar.Appearance.TintColor = UIColor.White; UIBarButtonItem.Appearance.TintColor = UIColor.White; return base.FinishedLaunching(app, options); }
如需区分深浅模式,可监听主题变化:
public override void TraitCollectionDidChange(UITraitCollection previousTraitCollection) { base.TraitCollectionDidChange(previousTraitCollection); var color = UITraitCollection.CurrentTraitCollection.UserInterfaceStyle == UIUserInterfaceStyle.Dark ? UIColor.White : UIColor.White; // 浅色模式也设为白色 UINavigationBar.Appearance.TintColor = color; }
验证测试
完成配置后,分别在各平台深浅模式下验证:
- Windows:汉堡、返回、更多图标均显示白色
- Android:深色模式下更多图标变为白色,返回按钮保持白色
- iOS:所有工具栏图标统一为白色
内容的提问来源于stack exchange,提问作者S Arslan
相关产品推荐
相关产品推荐

