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(); | |
} | |
} | |
} |
@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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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