MAUI窗口标题栏呈灰色且不跟随系统强调色的原因及解决方法
解决Windows 10下MAUI窗口标题栏不显示系统强调色的问题
问题根源
MAUI 默认的 Windows 平台标题栏实现,在 Windows 10 环境下不会自动关联系统强调色配置,需要通过平台特定代码手动对接系统颜色设置。
解决方案
1. 定位Windows平台代码文件
打开MAUI项目的 Platforms/Windows/App.xaml.cs 文件,我们将在这里添加全局标题栏配置逻辑。
2. 添加标题栏颜色绑定代码
在 OnLaunched 方法内,窗口初始化完成后插入以下代码:
#if WINDOWS using Microsoft.UI; using Microsoft.UI.Windowing; using Windows.UI; using Windows.UI.ViewManagement; using WinRT.Interop; protected override void OnLaunched(LaunchActivatedEventArgs args) { base.OnLaunched(args); var currentWindow = App.Current.Windows[0]; var windowHandle = WindowNative.GetWindowHandle(currentWindow.Handler.PlatformView); var windowId = Win32Interop.GetWindowIdFromWindow(windowHandle); var appWindow = AppWindow.GetFromWindowId(windowId); if (appWindow != null) { // 获取系统强调色 var uiSettings = new UISettings(); Color accentColor = uiSettings.GetColorValue(UIColorType.Accent); // 设置激活状态标题栏颜色 appWindow.TitleBar.BackgroundColor = accentColor; // 根据强调色亮度自动适配文字颜色 double brightness = (accentColor.R * 299 + accentColor.G * 587 + accentColor.B * 114) / 1000.0; appWindow.TitleBar.ForegroundColor = brightness > 128 ? Colors.Black : Colors.White; // 设置非激活状态标题栏颜色 appWindow.TitleBar.InactiveBackgroundColor = Color.FromArgb(200, accentColor.R, accentColor.G, accentColor.B); appWindow.TitleBar.InactiveForegroundColor = brightness > 128 ? Colors.DarkGray : Colors.LightGray; } } #endif
3. 验证效果
重新编译运行项目,此时窗口激活时标题栏会显示系统强调色,失去焦点时会呈现半透明的强调色变体,标题文字颜色也会根据强调色亮度自动适配,保证可读性。
注意事项
- 确保Windows 10设置中「在以下区域显示强调色」的选项已勾选(你已完成此操作)。
- 代码中的
#if WINDOWS指令会确保这段逻辑仅在Windows平台生效,不会影响Android、iOS等其他平台的编译。
内容的提问来源于stack exchange,提问作者Medinoc
相关产品推荐
相关产品推荐

