.net 如何在最大窗口中打开Maui项目

bttbmeg0  于 2022-12-24  发布在  .NET
关注(0)|答案(2)|浏览(214)

当我们运行.net MAUI项目时,它将在最小化窗口中打开该项目,但我想在最大化窗口中打开该项目。
在代码中我做了修改,使它将在Windows平台中打开一个最大化的窗口。

guykilcj

guykilcj1#

您可以使用WinUIEx NuGet包轻松实现这一点。
首先,在你的项目中安装WinUIEx。目前,我猜你需要安装v1.8.0。
然后将这些WINDOWS代码添加到MauiProgram.cs中。

#if WINDOWS
using WinUIEx;
using Microsoft.Maui.LifecycleEvents;
#endif

namespace MauiApp1;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            });

#if WINDOWS
        builder.ConfigureLifecycleEvents(events =>
        {
            events.AddWindows(wndLifeCycleBuilder =>
            {
                wndLifeCycleBuilder.OnWindowCreated(window =>
                {
                    window.Maximize();
                });
            });
        });
#endif

        return builder.Build();
    }
}
x3naxklr

x3naxklr2#

根据Github上的[HELP] How to specify a fixed size for the application window?,您可以尝试编辑MauiProgram.cs,如下所示,以便在初始化应用程序时在Windows平台中以最大化窗口打开。

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");
                  fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            });
#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 = 1920;
                        const int height = 1080;
                        winuiAppWindow.MoveAndResize(new RectInt32(1920 / 2 - width / 2, 1080 / 2 - height / 2, width, height));
                    }
                });
            });
        });
#endif
        return builder.Build();
    }
}

相关问题