Skip to content

Instantly share code, notes, and snippets.

@LanceMcCarthy
Last active August 30, 2022 05:40
Show Gist options
  • Select an option

  • Save LanceMcCarthy/c8d23f5ba6157d5d53bee3b1a741f8b5 to your computer and use it in GitHub Desktop.

Select an option

Save LanceMcCarthy/c8d23f5ba6157d5d53bee3b1a741f8b5 to your computer and use it in GitHub Desktop.

Revisions

  1. LanceMcCarthy revised this gist Jun 9, 2022. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions Example_A.MauiProgram.cs
    Original file line number Diff line number Diff line change
    @@ -29,17 +29,13 @@ public static MauiApp CreateMauiApp()
    {
    wndLifeCycleBuilder.OnWindowCreated(window =>
    {
    IntPtr nativeWindowHandle = WinRT.Interop.WindowNative.GetWindowHandle(window);
    WindowId win32WindowsId = Win32Interop.GetWindowIdFromWindow(nativeWindowHandle);
    AppWindow winuiAppWindow = AppWindow.GetFromWindowId(win32WindowsId);

    // 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;

    winuiAppWindow.MoveAndResize(new RectInt32(x, y, width, height));
    window.MoveAndResize(new RectInt32(x, y, width, height));
    });
    });
    });
  2. LanceMcCarthy revised this gist May 6, 2022. 2 changed files with 0 additions and 1 deletion.
    File renamed without changes.
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,6 @@ public static MauiApp CreateMauiApp()
    var builder = MauiApp.CreateBuilder();
    builder
    .UseMauiApp<App>()
    .UseTelerik()
    .ConfigureFonts(fonts =>
    {
    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
  3. LanceMcCarthy revised this gist May 6, 2022. No changes.
  4. LanceMcCarthy revised this gist May 6, 2022. 2 changed files with 55 additions and 0 deletions.
    54 changes: 54 additions & 0 deletions MacCatalystExample.MauiProgram.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    // ******** 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>()
    .UseTelerik()
    .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();
    }
    }
    }
    1 change: 1 addition & 0 deletions MauiProgram.cs → WinUI3Example.MauiProgram.cs
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // ******** WinUI3 Window management ******** //
    using Microsoft.Maui.LifecycleEvents;

    #if WINDOWS
  5. LanceMcCarthy created this gist Apr 25, 2022.
    50 changes: 50 additions & 0 deletions MauiProgram.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    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 =>
    {
    IntPtr nativeWindowHandle = WinRT.Interop.WindowNative.GetWindowHandle(window);
    WindowId win32WindowsId = Win32Interop.GetWindowIdFromWindow(nativeWindowHandle);
    AppWindow winuiAppWindow = AppWindow.GetFromWindowId(win32WindowsId);

    // 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;

    winuiAppWindow.MoveAndResize(new RectInt32(x, y, width, height));
    });
    });
    });
    #endif

    return builder.Build();
    }
    }
    }