Aleksandr
Aleksandr

Reputation: 89

How do you set the startup page size in dotnet MAUI for macOS(net6.0-maccatalyst)

There is already a solution for windows, I need something similar, but for MacOS; Solution for Windows: MAUI .NET Set Window Size

Upvotes: 2

Views: 2392

Answers (2)

Gizmo-Cal
Gizmo-Cal

Reputation: 21

I am not sure where you found this.; It works for me in MacOs, kudos. For me the complete solution in context. I added this function to MainPage.xaml.cs

Note: This is a MACOS specific feature. It needs platform specific wrapping.

Then I called the initial size I wanted in the mainpage function.

public MainPage()
{

    InitializeComponent();
                               
    SetMainWindowStartSize(300, 150);
}
private void SetMainWindowStartSize(int width, int height)
{
    Microsoft.Maui.Handlers.WindowHandler.Mapper.AppendToMapping(
        nameof(IWindow), (handler, view) =>
        {
            var size = new CoreGraphics.CGSize(width, height);
            handler.PlatformView.WindowScene.SizeRestrictions.MinimumSize = size;
            handler.PlatformView.WindowScene.SizeRestrictions.MaximumSize = size;
            Task.Run(() =>
            {
                Thread.Sleep(1000);
                MainThread.BeginInvokeOnMainThread(() =>
                {
                    handler.PlatformView.WindowScene.SizeRestrictions.MinimumSize = new CoreGraphics.CGSize(100, 100);
                    handler.PlatformView.WindowScene.SizeRestrictions.MaximumSize = new CoreGraphics.CGSize(5000, 5000);
                });
            });

        });
}

Upvotes: 2

Aleksandr
Aleksandr

Reputation: 89

I independently found the answer to the question, below I attached the code:

private void SetMainWindowStartSize(int width, int height)
{
    Microsoft.Maui.Handlers.WindowHandler.Mapper.AppendToMapping(
        nameof(IWindow), (handler, view) =>
        {
            var size = new CoreGraphics.CGSize(width, height);
            handler.PlatformView.WindowScene.SizeRestrictions.MinimumSize = size;
            handler.PlatformView.WindowScene.SizeRestrictions.MaximumSize = size;
            Task.Run(() =>
            {
                Thread.Sleep(1000);
                MainThread.BeginInvokeOnMainThread(() =>
                {
                    handler.PlatformView.WindowScene.SizeRestrictions.MinimumSize = new CoreGraphics.CGSize(100, 100);
                    handler.PlatformView.WindowScene.SizeRestrictions.MaximumSize = new CoreGraphics.CGSize(5000, 5000);
                });
            });

        });
}

Upvotes: 2

Related Questions