Skip to content

Instantly share code, notes, and snippets.

@LanceMcCarthy
Last active August 30, 2022 05:40
Show Gist options
  • 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

faisalkkv commented Jun 10, 2022

@LanceMcCarthy unable to use using WinUIEx; namespace in App.xaml.cs , its not compatible with .ne6-ios and android

@LanceMcCarthy
Copy link
Author

@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:

image

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

@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