-
-
Save LanceMcCarthy/c8d23f5ba6157d5d53bee3b1a741f8b5 to your computer and use it in GitHub Desktop.
| // ******** WinUI3 Window management ******** // | |
| using Microsoft.Maui.LifecycleEvents; | |
| #if WINDOWS | |
| using Microsoft.UI; | |
| using Microsoft.UI.Windowing; | |
| using Windows.Graphics; | |
| #endif | |
| namespace MyApp.Maui | |
| { | |
| 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 => | |
| { | |
| // Hard coded logic to center window on a 1920x1080 display, adjust as needed | |
| const int width = 1200; | |
| const int height = 800; | |
| const int x = 1920 / 2 - width / 2; | |
| const int y = 1080 / 2 - height / 2; | |
| window.MoveAndResize(new RectInt32(x, y, width, height)); | |
| }); | |
| }); | |
| }); | |
| #endif | |
| return builder.Build(); | |
| } | |
| } | |
| } |
| // ******** MacCatalyst Window management ******** // | |
| using Microsoft.Maui.LifecycleEvents; | |
| #elif MACCATALYST | |
| using AppKit; | |
| using CoreGraphics; | |
| using Foundation; | |
| using UIKit; | |
| #endif | |
| namespace Hacked.Maui | |
| { | |
| 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"); | |
| }); | |
| builder.ConfigureLifecycleEvents(events => | |
| { | |
| #elif MACCATALYST | |
| events.AddiOS(wndLifeCycleBuilder => | |
| { | |
| wndLifeCycleBuilder.SceneWillConnect((scene, session, options) => | |
| { | |
| if (scene is UIWindowScene { SizeRestrictions: { } } windowScene) | |
| { | |
| windowScene.SizeRestrictions.MaximumSize = new CGSize(1200, 900); | |
| windowScene.SizeRestrictions.MinimumSize = new CGSize(600, 400); | |
| } | |
| }); | |
| }); | |
| #endif | |
| }); | |
| return builder.Build(); | |
| } | |
| } | |
| } |
@faisalkkv You could just use WinUIEx's myWindow.CenterOnScreen(); method without needing dimensions https://github.com/dotMorten/WinUIEx/blob/main/docs/concepts/WindowExtensions.md#window-extension-methods
@LanceMcCarthy unable to use using WinUIEx; namespace in App.xaml.cs , its not compatible with .ne6-ios and android
@faisalkkv You're probably in the wrong file. that code goes into MauiProgram.cs, not App.xaml.cs
also, make sure you use @if statements for the windows-only code:
If you're not sure what is going on, check out the winUIEx docs https://github.com/dotMorten/WinUIEx/blob/main/docs/concepts/Maui.md
@LanceMcCarthy Thanks for you support, its done !!!!
@faisalkkv woot! Just in case it's helpful, I have checked in a full example in one of my demo repositories.
- See here for the extension method's use https://github.com/LanceMcCarthy/DevOpsExamples/blob/main/src/MAUI/MauiDemo/MauiProgram.cs
- See here for the required NuGet package https://github.com/LanceMcCarthy/DevOpsExamples/blob/ccc975c8da1999b16cc64b690d857ff8dec6e920/src/MAUI/MauiDemo/MauiDemo.csproj#L69

how do we get the display size automatically ? without hardcode