.net MAUI如何在启动时最大化应用程序。

4

我该如何让 .net MAUI 应用在 Windows 上启动时最大化窗口? 目前它以小窗口启动,我不希望用户不断地将其最大化。

谢谢。

2个回答

7

试一下这个:

public App()
{
        this.InitializeComponent();

        Microsoft.Maui.Handlers.WindowHandler.Mapper.AppendToMapping(nameof(IWindow), (handler, view) =>
        {
#if WINDOWS
            var nativeWindow = handler.PlatformView;
            nativeWindow.Activate();
            IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(nativeWindow);
            ShowWindow(windowHandle, 3);
#endif
        });

      
    }
#if WINDOWS
    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, int cmdShow);
#endif
}

你的回答可以通过提供更多支持信息来改进。请添加更多细节,例如引用或文档,以便他人可以确认您的答案是正确的。您可以在帮助中心找到有关如何编写良好答案的更多信息。 - Saeid Amini
将上述代码添加到app.xaml.cs中,唯一不同的是在this.InitializeComponent();之后赋值给MainPage = new AppShell();。应用现在启动时是最大化的。谢谢。 - Caevan Sachinwalla
这对我也起作用了,就像魔法一样。下面的答案没有。谢谢!我已经搜索了几个小时才得到这个答案。 - Keltanis

5

maui团队必须等待winui团队实现任何缺失的功能,以便他们可以访问Windows特定属性,但是这github discussion显示了一些解决方法,您可以将其插入到MauiApp.CreateBuilder()方法中。

如果应用程序在Windows上运行,则此解决方法调用Windows本机服务。从那里,您可以插入任何WinUI3方法,但我对此一无所知。我采用了LanceMcCarthy的答案,在启动时最大化窗口或恢复其设置大小,如果该Presenter不正确,则保留它。我不知道winuiAppWindow.Presenter是否会不是OverlapPresenter,但我仍然将其保留在其中。

这在我的当前VS2022 17.3 Preview 1 maui RC3版本上运行,在Windows 11上运行。

using Microsoft.Maui.LifecycleEvents;

#if WINDOWS
using Microsoft.UI;
using Microsoft.UI.Windowing;
using Windows.Graphics;
#endif

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 =>
                {
                    IntPtr nativeWindowHandle = WinRT.Interop.WindowNative.GetWindowHandle(window);
                    WindowId win32WindowsId = Win32Interop.GetWindowIdFromWindow(nativeWindowHandle);
                    AppWindow winuiAppWindow = AppWindow.GetFromWindowId(win32WindowsId);
                    if(winuiAppWindow.Presenter is OverlappedPresenter p)
                        p.Maximize();
                    else
                    {
                        const int width = 1200;
                        const int height = 800;
                        winuiAppWindow.MoveAndResize(new RectInt32(1920 / 2 - width / 2, 1080 / 2 - height / 2, width, height));
                    }
                });
            });
        });
#endif
        return builder.Build();
    }
}

在我涉足 Maui 的几个月内,WinUI 已经有了很多进展,并且还有更多的计划(WinUI路线图)。因此,任何主要的短板都有可能很快得到解决,特别是现在 Maui 即将面向普通用户提供服务。


1
看起来对于一个非常简单的功能而言过于复杂了。我想我现在会转向一些 winForm + WebView2 + Blazor,因为我只针对桌面应用程序,并且已经在使用 MAUI Blazor 了。感谢您的回复! - pascx64

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接