Last active
August 30, 2022 05:40
-
-
Save LanceMcCarthy/c8d23f5ba6157d5d53bee3b1a741f8b5 to your computer and use it in GitHub Desktop.
MAUI Window Position and Size (WinUI3 and MacCatalyst)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ******** 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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ******** 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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@faisalkkv woot! Just in case it's helpful, I have checked in a full example in one of my demo repositories.