skip to Main Content

In MAUI apps, how can I completely disable window resize and fix the width and height to something like 200px?

Visual Studio automatically generates this code:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }
}

The Height and Width properties are readonly. I have found a RequestedWidth but it doesn’t change anything. When I try to change the base class to something else, I get an error saying that is not authorized.

I don’t really care if fixing the size does not make sense in Android or similar operating systems, it is important in Windows. So is there any way to do this?

2

Answers


  1. For windows, after the font settings, you can add this code to your MauiProgram.cs file. This will disable resize. Optionally you can change border and title bar visibility too. If you don’t disable title bar and border, buttons in the upper right corner of the window will be still visible and users still be able to switch your app to full screen mode. I recommend you to disable them too. Finally if you want to disable title bar you should set ExtendsContentIntoTitleBar property to false too. If you don’t, you’ll still see half the title bar.

        #if WINDOWS
            builder.ConfigureLifecycleEvents(events =>
            {
                events.AddWindows(windowsLifecycleBuilder =>
                {
                    windowsLifecycleBuilder.OnWindowCreated(window =>
                    {
                        window.ExtendsContentIntoTitleBar = false;
                        var handle = WinRT.Interop.WindowNative.GetWindowHandle(window);
                        var id = Win32Interop.GetWindowIdFromWindow(handle);
                        var appWindow = AppWindow.GetFromWindowId(id);
                        switch (appWindow.Presenter)
                        {
                            case OverlappedPresenter overlappedPresenter:
                                overlappedPresenter.SetBorderAndTitleBar(false, false);
                                overlappedPresenter.IsResizable = false;
                                break;
                        }
                    });
                });
            });
        #endif
    

    Now your Window size is fixed but it’s height & width still at default value. You should add this code to your MainPage.xaml.cs;

        protected override void OnAppearing()
        {
            base.OnAppearing();
    
            double height = 600;
            double width = 365;
    
            Window.Height = height;
            Window.Width = width;
    
        }
    
    Login or Signup to reply.
  2. You can rewrite the CreateWindow method in the App class to create a new window, set the maximum and minimum width and height, refer to the following code:

    public partial class App : Application
    {
           public App()
           {
                  InitializeComponent();
                  //MainPage = new AppShell();
           }
    
        protected override Window CreateWindow(IActivationState activationState)
        {
            var window = new Window();
            window.MaximumHeight = 500;
            window.MaximumWidth = 500;
            window.MinimumHeight = 500;
            window.MinimumWidth = 500;
            window.Page =new AppShell();
            return window;
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search