Skip to content

Instantly share code, notes, and snippets.

@LanceMcCarthy
Last active August 30, 2022 05:40
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LanceMcCarthy/c8d23f5ba6157d5d53bee3b1a741f8b5 to your computer and use it in GitHub Desktop.
Save LanceMcCarthy/c8d23f5ba6157d5d53bee3b1a741f8b5 to your computer and use it in GitHub Desktop.
MAUI Window Position and Size (WinUI3 and MacCatalyst)
// ******** 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
Copy link

@LanceMcCarthy Thanks for you support, its done !!!!

@LanceMcCarthy
Copy link
Author

@faisalkkv woot! Just in case it's helpful, I have checked in a full example in one of my demo repositories.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment