MAUI如何去掉标题栏并固定窗口大小?

Яро*_*кий 21 c# winui-3 maui .net-6.0

如何删除 MAUI 中的标题栏并将 Windows 版本应用程序中的窗口大小固定为 800x400 像素?

标题栏 全尺寸

我在网上查了很长时间,但发现一年多前发布的MAUI后续版本已经没有实际信息了。为什么 MAUI 不支持窗口调整大小并禁用其缩放作为条件 WPF,它还使用 XAML 来创建窗口,我希望发布时有这样的可能性。

标题栏看起来很破损,因为它比关闭/折叠/最大化按钮高。

Has*_*ruç 25

当标题为空时,则不显示上部栏。像这样:

Title=""
Run Code Online (Sandbox Code Playgroud)

像这样:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Chatfri.Pages.Settings"
             Title="">
    <StackLayout>
        <Label Text="Welcome to Settings!"
                VerticalOptions="Center" 
                HorizontalOptions="Center" />
    </StackLayout>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

如果您使用 shell,则可以使用 Shell.NavBarIsVisible="False"。

<Shell
    x:Class="Chatfri.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:Chatfri"
    xmlns:loc="clr-namespace:Chatfri.Pages"
    Shell.FlyoutBehavior="Disabled"
    Shell.NavBarIsVisible="False">

    <TabBar>
        <Tab Icon="home" Title="Home">
            <ShellContent
        
        ContentTemplate="{DataTemplate loc:Home}"
        Route="Home" />
        </Tab>
        <Tab Icon="messages" Title="Messages">
            <ShellContent
        
        ContentTemplate="{DataTemplate loc:Messages}"
        Route="Messages" />
        </Tab>
        <Tab Icon="settings" Title="Settings">
            <ShellContent
        
        ContentTemplate="{DataTemplate loc:Settings}"
        Route="Settings" />
        </Tab>
    </TabBar>

</Shell>
Run Code Online (Sandbox Code Playgroud)

  • 这会隐藏标题栏下方的导航栏,但不会隐藏标题栏本身。 (2认同)

Édg*_*dón 16

不是在 shell 本身中,而是在shell 内显示的页面中,您应该将该Shell.NavBarIsVisible属性设置为 false,如下所示:

<ContentPage
    ...
    Shell.NavBarIsVisible="False" />
Run Code Online (Sandbox Code Playgroud)


Moi*_*ruz 7

您可以阅读文档SetBorderAndTitleBarResize

SetBorderAndTitleBar(Boolean, Boolean) 设置窗口的边框和标题栏属性。

Resize(SizeInt32) 将窗口大小调整为指定大小。

你的 MauiProgram.cs 应该是这样的

    using Microsoft.Maui.LifecycleEvents;
    #if WINDOWS
    using Microsoft.UI;
    using Microsoft.UI.Windowing;
    using Windows.Graphics;
    #endif
    
    namespace YourNameSpace
    {
        public static class MauiProgram
        {
            
            public static MauiApp CreateMauiApp()
            {
                var builder = MauiApp.CreateBuilder();
                builder
                    .UseMauiApp<App>()
                    .ConfigureFonts(fonts =>
                    {
                        fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                    });
    #if WINDOWS
            builder.ConfigureLifecycleEvents(events =>
            {
                events.AddWindows(wndLifeCycleBuilder =>
                {
                    wndLifeCycleBuilder.OnWindowCreated(window =>
                    {
                        window.ExtendsContentIntoTitleBar = false; /*This is important to prevent your app content extends into the title bar area.*/
                        IntPtr nativeWindowHandle = WinRT.Interop.WindowNative.GetWindowHandle(window);
                        WindowId win32WindowsId = Win32Interop.GetWindowIdFromWindow(nativeWindowHandle);
                        AppWindow winuiAppWindow = AppWindow.GetFromWindowId(win32WindowsId);
                        if(winuiAppWindow.Presenter is OverlappedPresenter p)
                        {
                            p.SetBorderAndTitleBar(false, false);
                        }
const int width = 1200;
                        const int height = 800;
/*I suggest you to use MoveAndResize instead of Resize because this way you make sure to center the window*/
                        winuiAppWindow.MoveAndResize(new RectInt32(1920 / 2 - width / 2, 1080 / 2 - height / 2, width, height));
                    });
                });
            });
    #endif
                builder.Services.AddMauiBlazorWebView();
                return builder.Build();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

但您具体需要的代码是在预处理器指令中找到的代码

#if WINDOWS
Run Code Online (Sandbox Code Playgroud)


Ger*_*uis 4

这是一个已知的错误,目前已开放 PR ,合并后将被修复。