How can I disable window resizing for the user in NET MAUI?

102 views Asked by At

I have the following C# code for Windows platform:

namespace FigureCollapse.WinUI
{
    public partial class App : MauiWinUIApplication
    {
        public App()
        {
            //AssignAppData() is responsible for selecting the application's data storage folder depending on the platform.
            AppData.AsignAppData(ApplicationData.Current.LocalFolder.Path, true);

            //SelectAppLang() is responsible for selecting the language of the app depending on the configuration file stored in AppData
            Lang.SelectAppLang(AppData.AppConfig["Language"]);

            this.InitializeComponent();
        }

        protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
    }
}

I want to be able to do four things:

  1. That the user cannot change the size of the window.
  2. That depending on the configuration file data stored in AppData (in this case it would be AppConfig["Resolution"]) is the size of the displayed window.
  3. Depending on the data in the configuration file, it is known whether the app is in window mode or full screen mode.
  4. Disable the option to maximize/minimize app window size

I was trying for a long time how to do it but I couldn't with the first point.

I tried searching the documentation, Google and chatGPT. The documentation didn't mention much, Google didn't show me results, and ChatGPT gave me broken, non-working code.

1

There are 1 answers

1
Simon Mourier On

You can go to the App.xaml.cs in Platforms\Windows directory and use WinUI3's AppWindow feature.

So, that would be something like this:

using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;
using Microsoft.UI.Windowing;
using Windows.Graphics;

namespace MyMauiApp.WinUI
{
    public partial class App : MauiWinUIApplication
    {
        public App()
        {
            InitializeComponent();
            WindowHandler.Mapper.AppendToMapping(nameof(IWindow), (handler, view) =>
            {
                var window = handler.PlatformView.GetAppWindow();
                if (window != null)
                {
                    // resize & center to something we like
                    var display = DisplayArea.GetFromWindowId(window.Id, DisplayAreaFallback.Nearest);
                    const int width = 480;
                    const int height = 640;
                    window.MoveAndResize(new RectInt32((display.WorkArea.Width - width) / 2, (display.WorkArea.Height - height) / 2, width, height));

                    // prevent resizing
                    if (window.Presenter is OverlappedPresenter presenter)
                    {
                        presenter.IsResizable = false;
                        presenter.IsMaximizable = false;
                        presenter.IsMinimizable = false;
                    }
                }
            });
        }

        protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
    }
}

To go to fullscreen (or quit it) you use AppWindowPresenterKind Enum like this for example:

window.SetPresenter(AppWindowPresenterKind.FullScreen);