【问题标题】:.net MAUI how to maximize application on startup.net MAUI 如何在启动时最大化应用程序
【发布时间】:2023-01-03 00:32:07
【问题描述】:

如何使 .net MAUI 应用程序在启动时最大化 Windows 上的窗口? 目前它是从一个小窗口开始的,我不希望用户必须不断地最大化它。

谢谢,

【问题讨论】:

    标签: .net maui maui-windows


    【解决方案1】:

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

    如果应用程序在 Windows 上运行,解决方法将调用 Windows 本机服务。从那里您可以插入任何 WinUI3 方法,但这是我根本不熟悉的东西。我采用了 LanceMcCarthy 的答案,以在启动时最大化窗口或在演示者不正确时恢复他的设置大小。我不知道 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 roadmap) 因此,任何重大的缺点很可能很快就会得到解决,尤其是 maui 现在随时都将全面上市.

    【讨论】:

    • 对于一个非常简单的功能来说似乎过于复杂了 ?。我想我现在会切换到一些 winForm + WebView2 + Blazor,因为无论如何我只针对桌面并且已经在使用 MAUI Blazor。谢谢回复!
    【解决方案2】:

    试试这个:

    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
    }
    

    【讨论】:

    • 您的答案可以通过其他支持信息得到改进。请添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写出好的答案的信息in the help center
    • 将以上代码添加到 app.xaml.cs 中,我的代码中唯一的区别是在 this.InitializeComponent(); 之后分配给 MainPage = new AppShell(); 应用程序现在启动最大化。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 2017-02-21
    • 2020-07-24
    • 2022-10-05
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    相关资源
    最近更新 更多