4

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,

pascx64
  • 904
  • 16
  • 31

2 Answers2

6

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
}
Juan Pablo Gomez
  • 5,203
  • 11
  • 55
  • 101
José Chaudary
  • 119
  • 2
  • 7
  • 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](https://stackoverflow.com/help/how-to-answer) – Saeid Amini Sep 10 '22 at 03:06
  • 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 – Caevan Sachinwalla Sep 29 '22 at 03:21
  • 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. – Keltanis Jan 11 '23 at 06:22
4

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.

spencer
  • 121
  • 2
  • 3
  • 1
    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! – pascx64 May 28 '22 at 00:09