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

MAUI应用最大化时自定义标题栏颜色失效问题求助

修复MAUI WinUI标题栏最大化状态下颜色失效问题

问题根源

  1. 窗口最大化时,WinUI标题栏会与内容区域融合,若未同步设置窗口背景色,会出现顶部灰色间隙。
  2. 仅配置按钮背景色未覆盖前景色属性,导致按钮在hover/pressed状态下出现颜色异常切换。

修复后的代码

修改WinUIPlatformService的ApplyTitleBarColor方法,补充关键配置:

using Microsoft.Maui.Platform;
using uiXaml = Microsoft.UI.Xaml;
using windowing = Microsoft.UI.Windowing;

namespace MauiTesting2.Platforms.Windows;
public class WinUIPlatformService : IPlatformService {

    private uiXaml.Window NativeWindow => (uiXaml.Window)App.Current.Windows.First().Handler.PlatformView;
    private windowing.AppWindow m_appWindow = null;
    private windowing.AppWindow AppWindow {
        get {
            if (m_appWindow == null) {
                var windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(this.NativeWindow);
                var windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(windowHandle);
                m_appWindow = windowing.AppWindow.GetFromWindowId(windowId);
            }
            return m_appWindow;
        }
    }

    private windowing.AppWindowTitleBar m_titleBar = null;
    private windowing.AppWindowTitleBar TitleBar => m_titleBar ?? (m_titleBar = this.AppWindow.TitleBar);

    public void ApplyTitleBarColor(Color toColor) {
        var winUiColor = toColor.ToWindowsColor();
        // 标题栏背景全状态配置
        this.TitleBar.InactiveBackgroundColor = winUiColor;
        this.TitleBar.BackgroundColor = winUiColor;
        this.TitleBar.ButtonBackgroundColor = winUiColor;
        this.TitleBar.ButtonHoverBackgroundColor = winUiColor;
        this.TitleBar.ButtonInactiveBackgroundColor = winUiColor;
        this.TitleBar.ButtonPressedBackgroundColor = winUiColor;

        // 修复按钮状态颜色切换问题(按需调整前景色)
        var foregroundColor = Windows.UI.Colors.White;
        this.TitleBar.ButtonForegroundColor = foregroundColor;
        this.TitleBar.ButtonHoverForegroundColor = foregroundColor;
        this.TitleBar.ButtonPressedForegroundColor = foregroundColor;
        this.TitleBar.ButtonInactiveForegroundColor = foregroundColor;
        this.TitleBar.ForegroundColor = foregroundColor;
        this.TitleBar.InactiveForegroundColor = foregroundColor;

        // 同步窗口背景色,消除最大化时顶部灰色间隙
        NativeWindow.Background = new uiXaml.Media.SolidColorBrush(winUiColor);
    }
}

核心修复点

  • 同步窗口背景色:将WinUI原生窗口的背景色与标题栏设置为一致,解决最大化状态下的顶部间隙问题。
  • 完整覆盖标题栏状态:显式配置标题栏及按钮的所有激活/非激活、hover/pressed状态颜色,避免系统默认颜色干扰。

替代方案:直接在WinUI启动逻辑中配置

若无需封装平台服务,可直接在Platforms/Windows/App.xaml.cs中初始化标题栏:

protected override void OnLaunched(LaunchActivatedEventArgs args)
{
    base.OnLaunched(args);

    var window = Application.Current.Windows.First();
    var nativeWindow = (uiXaml.Window)window.Handler.PlatformView;
    var windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(nativeWindow);
    var windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(windowHandle);
    var appWindow = windowing.AppWindow.GetFromWindowId(windowId);
    var titleBar = appWindow.TitleBar;

    var targetColor = Colors.Red.ToWindowsColor();
    // 标题栏背景配置
    titleBar.BackgroundColor = targetColor;
    titleBar.InactiveBackgroundColor = targetColor;
    titleBar.ButtonBackgroundColor = targetColor;
    // 按钮前景配置
    titleBar.ButtonForegroundColor = Windows.UI.Colors.White;
    titleBar.ButtonHoverForegroundColor = Windows.UI.Colors.White;
    // 窗口背景同步
    nativeWindow.Background = new uiXaml.Media.SolidColorBrush(targetColor);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 21:22:42