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

如何在.NET MAUI中调整标题栏及最小化、关闭按钮的大小?

调整.NET MAUI应用标题栏返回、最小化及关闭按钮大小的解决方案

针对Windows平台的标题栏按钮调整,以下提供两种可行方案:

方案一:修改系统标题栏按钮尺寸(仅Windows)

系统标题栏的按钮属于WinUI原生控件,可通过调用WinAPI直接调整参数:

在页面的代码后台(如MainPage.xaml.cs)中添加以下代码:

protected override void OnHandlerChanged()
{
    base.OnHandlerChanged();

#if WINDOWS
    var windowHandler = (Microsoft.Maui.Handlers.WindowHandler)Application.Current.Windows[0].Handler;
    var appWindow = windowHandler.PlatformWindow;
    var titleBar = appWindow.TitleBar;

    // 调整按钮宽度(默认值为46,按需修改)
    titleBar.ButtonWidth = 50;
    // 调整标题栏高度,按钮高度会随标题栏高度自适应
    titleBar.Height = 50;

    // 可选:调整按钮颜色样式
    titleBar.ButtonForegroundColor = Microsoft.UI.Colors.Black;
    titleBar.ButtonHoverForegroundColor = Microsoft.UI.Colors.DarkGray;
#endif
}

方案二:自定义标题栏(跨平台通用)

若系统标题栏的调整无法满足需求,可隐藏默认标题栏,自行实现包含控制按钮的自定义标题栏:

1. 隐藏默认标题栏

在App.xaml.cs中禁用默认导航栏:

public App()
{
    InitializeComponent();
    MainPage = new NavigationPage(new MainPage());
    NavigationPage.SetHasNavigationBar(MainPage, false);
}

2. 编写自定义标题栏XAML

在页面的XAML中添加自定义标题栏布局:

<Grid>
    <!-- 自定义标题栏 -->
    <Grid HeightRequest="50" BackgroundColor="#EEEEEE">
        <!-- 返回按钮 -->
        <Button Text="<" WidthRequest="50" HeightRequest="50" 
                Clicked="BackButton_Clicked" />
        <!-- 应用标题 -->
        <Label Text="我的应用" HorizontalOptions="Center" VerticalOptions="Center" />
        <!-- 窗口控制按钮组 -->
        <StackLayout Orientation="Horizontal" HorizontalOptions="End">
            <Button Text="_" WidthRequest="50" HeightRequest="50" 
                    Clicked="MinimizeButton_Clicked" />
            <Button Text="□" WidthRequest="50" HeightRequest="50" 
                    Clicked="MaximizeButton_Clicked" />
            <Button Text="X" WidthRequest="50" HeightRequest="50" 
                    Clicked="CloseButton_Clicked" />
        </StackLayout>
    </Grid>
    <!-- 页面内容区域 -->
    <ScrollView Margin="0,50,0,0">
        <!-- 此处放置你的页面内容 -->
    </ScrollView>
</Grid>

3. 实现按钮功能逻辑

在页面代码后台添加按钮点击事件处理:

#if WINDOWS
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
#endif

private void BackButton_Clicked(object sender, EventArgs e)
{
    // 处理页面返回逻辑
    if (Navigation.NavigationStack.Count > 1)
    {
        Navigation.PopAsync();
    }
}

private void MinimizeButton_Clicked(object sender, EventArgs e)
{
#if WINDOWS
    var windowHandler = (Microsoft.Maui.Handlers.WindowHandler)Application.Current.Windows[0].Handler;
    windowHandler.PlatformWindow.AppWindow.Minimize();
#endif
}

private void MaximizeButton_Clicked(object sender, EventArgs e)
{
#if WINDOWS
    var windowHandler = (Microsoft.Maui.Handlers.WindowHandler)Application.Current.Windows[0].Handler;
    if (windowHandler.PlatformWindow.AppWindow.Presenter is OverlappedPresenter presenter)
    {
        presenter.State = presenter.State == OverlappedPresenterState.Maximized 
            ? OverlappedPresenterState.Restored 
            : OverlappedPresenterState.Maximized;
    }
#endif
}

private void CloseButton_Clicked(object sender, EventArgs e)
{
#if WINDOWS
    var windowHandler = (Microsoft.Maui.Handlers.WindowHandler)Application.Current.Windows[0].Handler;
    windowHandler.PlatformWindow.AppWindow.Close();
#endif
}

注意事项

  • 方案一仅适用于Windows平台,Android、iOS等平台的系统标题栏按钮样式由系统管控,无法直接通过MAUI调整。
  • 方案二可跨平台使用,但需针对不同平台适配窗口控制逻辑(如Android可直接使用系统返回键,无需自定义)。

内容的提问来源于stack exchange,提问作者Genesis Rei Viñalon

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 12:17:54