.net MAUI how to maximize application on startup
Asked Answered
S

2

5

How can I make a .net MAUI app maximize the window on Windows when it's launched ? Currently it is starting as a small window and I don't want the user to have to constantly maximize it.

Thanks,

Standardbearer answered 5/5, 2022 at 14:4 Comment(0)
P
7

The maui team has to wait on the winui team to implement any missing features so they can access Windows specific attributes, but this github discussion shows some workarounds that you can plug into your MauiApp.CreateBuilder() method.

The workaround calls the windows native services if the application is running on windows. From there you can plug in any WinUI3 methods, but that's something I'm not familiar with at all. I adopted the answer from LanceMcCarthy to maximize the window on startup or resume his set size if that presenter isn't right. Idk if the winuiAppWindow.Presenter would ever not be an OverlapPresenter, but I left it in there anyway.

This works on my current VS2022 17.3 Preview 1 maui RC3 version, running on 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();
    }
}

There's been a bunch of winui developments in just the couple months i've been dabbling in maui, and more planned (winui roadmap ) So chances are any major short comings will be fixed soon, especially with maui heading for general availability any day now.

Phylloquinone answered 24/5, 2022 at 0:23 Comment(2)
Seems crazy overcomplicated for a really simple feature πŸ˜…. I think I will switch to some winForm + WebView2 + Blazor for now since I'm only targeting desktop anyway and already using MAUI Blazor. Thanks for the reply! – Standardbearer
I switched completely from Microsoft robust coding to electronjs.org – Handkerchief
D
9

Try this:

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
}
Delano answered 6/9, 2022 at 6:8 Comment(3)
Your answer could be improved with additional supporting information. Please add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center – Security
Added the above code to app.xaml.cs only difference in my code is an assignment to MainPage = new AppShell(); after this.InitializeComponent(); The Application now starts up maximised. Thank you – Capriola
This worked for me as well. like a charm. The below answer did not. Thanks! I had been searching for hours to get this answer. – Adrastus
P
7

The maui team has to wait on the winui team to implement any missing features so they can access Windows specific attributes, but this github discussion shows some workarounds that you can plug into your MauiApp.CreateBuilder() method.

The workaround calls the windows native services if the application is running on windows. From there you can plug in any WinUI3 methods, but that's something I'm not familiar with at all. I adopted the answer from LanceMcCarthy to maximize the window on startup or resume his set size if that presenter isn't right. Idk if the winuiAppWindow.Presenter would ever not be an OverlapPresenter, but I left it in there anyway.

This works on my current VS2022 17.3 Preview 1 maui RC3 version, running on 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();
    }
}

There's been a bunch of winui developments in just the couple months i've been dabbling in maui, and more planned (winui roadmap ) So chances are any major short comings will be fixed soon, especially with maui heading for general availability any day now.

Phylloquinone answered 24/5, 2022 at 0:23 Comment(2)
Seems crazy overcomplicated for a really simple feature πŸ˜…. I think I will switch to some winForm + WebView2 + Blazor for now since I'm only targeting desktop anyway and already using MAUI Blazor. Thanks for the reply! – Standardbearer
I switched completely from Microsoft robust coding to electronjs.org – Handkerchief

© 2022 - 2024 β€” McMap. All rights reserved.