1

When We run the .net MAUI Project it will open the project in minimized windows but I want to open that in the maximized window.

Where in the code I made changes so it will open in a maximized window in the Windows platform.

2 Answers 2

2

Per [HELP] How to specify a fixed size for the application window? on Github, you can try to edit your MauiProgram.cs like below to open in a maximized window in the Windows platform when initializing the app.

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();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This code gives me the error: "Error CS1061 'MauiAppBuilder' does not contain a definition for 'ConfigureLifecycleEvents' and no accessible extension method 'ConfigureLifecycleEvents' accepting a first argument of type 'MauiAppBuilder' could be found". But that's not true, because I can directly access builder.ConfigureLifecycleEvents outside of the conditional block. Also the conditional block's code is all grey colored, not sure what that means.
This is a better solution, because it does not require the WinUIEx library which is supporting only net6.0-windows10.0.18362
0

You can easily achieve this with the WinUIEx NuGet package.

First of all, install WinUIEx in your project. At the moment, I guess you need to install v1.8.0 I guess.

Then add these WINDOWS code in 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();
    }
}

1 Comment

WinUIEx library is supported only on net6.0-windows10.0.18362 if you are using net7.0 then this solution will not work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.