Maui Blazor Windows项目:如何移除默认窗口栏?
在MAUI Blazor Windows平台移除默认窗口栏的解决方案
方法1:通过App.xaml.cs配置窗口
打开Platforms/Windows/App.xaml.cs,修改OnLaunched方法,添加WinUI窗口的自定义设置:
using Microsoft.UI.Xaml; namespace YourAppName.Windows; public partial class App : MauiWinUIApplication { // 可选:保存WinUI窗口实例,用于后续拖动等操作 public static Window WinUIWindow { get; private set; } protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp(); protected override void OnLaunched(LaunchActivatedEventArgs args) { base.OnLaunched(args); // 获取WinUI窗口实例 WinUIWindow = Application.Current.Windows[0].Handler.PlatformView as Window; if (WinUIWindow != null) { // 允许应用内容延伸到标题栏区域 WinUIWindow.ExtendsContentIntoTitleBar = true; // 隐藏默认系统标题栏 WinUIWindow.TitleBar.Visibility = Visibility.Collapsed; } } }
方法2:通过MauiProgram.cs的生命周期事件配置
如果你更倾向于在应用启动配置中统一处理,可修改MauiProgram.cs:
using Microsoft.Maui.LifecycleEvents; namespace YourAppName; public static class MauiProgram { public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder(); builder .UseMauiApp<App>() .ConfigureFonts(fonts => { fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); }); // 配置Windows平台生命周期事件 builder.ConfigureLifecycleEvents(events => { #if WINDOWS events.AddWindows(winBuilder => { winBuilder.OnWindowCreated(window => { var winUIWindow = window as Microsoft.UI.Xaml.Window; if (winUIWindow != null) { winUIWindow.ExtendsContentIntoTitleBar = true; winUIWindow.TitleBar.Visibility = Microsoft.UI.Xaml.Visibility.Collapsed; } }); }); #endif }); builder.Services.AddMauiBlazorWebView(); #if DEBUG builder.Services.AddBlazorWebViewDeveloperTools(); #endif return builder.Build(); } }
补充:实现自定义顶部栏的窗口拖动功能
移除默认标题栏后,需要给你的自定义顶部栏添加拖动窗口的能力,在Blazor组件中处理:
<!-- 你的自定义顶部栏组件 --> <div class="custom-top-bar" @onmousedown="DragWindow"> <!-- 你的按钮、标题等元素 --> <button>返回</button> <span>自定义标题</span> </div> @code { private void DragWindow() { #if WINDOWS // 调用WinUI窗口的拖动方法 App.WinUIWindow?.DragMove(); #endif } }
注意事项
- 确保自定义顶部栏有足够高度,避免遮挡应用内容;
- 如果需要窗口的最小化/最大化/关闭按钮,要在自定义顶部栏中自行实现,并通过WinUI API调用对应操作(比如
WinUIWindow.Close()、WinUIWindow.TryMinimize()等)。
内容的提问来源于stack exchange,提问作者Samih EL SAKHAWI
相关产品推荐
相关产品推荐

