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.
CodePudding user response:
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();
}
}
CodePudding user response:
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();
}
}