Full-screen, disable minimize/maximize for .NET MAUI Windows apps
Under one of my videos I got the question: how do I enable or disable minimizing and maximizing the application window for my Windows app with .NET MAUI? That’s exactly what we will learn in this post!
While all of this can probably also be achieved on macOS and possibly other platforms, I am focussing on Windows for this post. You can find all the code in a accompanying GitHub repository: https://github.com/jfversluis/MauiWindowsFullscreenMinimizeMaximizeSample
Multi-Window Support .NET MAUI #
In case you didn’t know: .NET MAUI has support for working with windows and also working with multiple windows. While this is mostly useful in desktop scenarios with Windows and macOS, you can also use it on other platforms.
There is all kinds of properties and events that you can now work with to take advantage of this functionality. Read all about it on Microsoft Learn. You can also review my video about some of this below.
My video about some of the desktop specific .NET MAUI APIs, including window support
Fullscreen Window #
While this wasn’t part of the initial question, I did decide to also add a little description on how to make a window fullscreen. Fullscreen in this case meaning: no close button, maximize or minimize button (aka the titlebar) and no taskbar. This would allow your app to run as a so-called kiosk mode if that’s what you want.
Observe the code to achieve this below, I will walk you through it, no worries.
The code you put in the MauiProgram.cs to make the main window open fullscreen on Windows.
The first thing that stands out is that you need to wrap this in
#if WINDOWS compiler directives. There are definitely other ways to solve this, but this is the easiest.
The next thing that is good to know is that we hook this up in the .NET MAUI generic host builder by hooking into
OnWindowCreated respectively. By doing it like this, every window that gets created will run the code we specify next. In our case that means every window will be opened fullscreen.
If this is something you don’t want you might want to make your own inheritance of the
Window object and check first if you’re dealing with a certain variation before applying certain logic to it.
Making the Window Fullscreen #
The actual logic to make the window fullscreen is not really that straight-forward. WinUI, the framework .NET MAUI is building upon to enable Windows support, is also still being developed and sometimes has some crude ways of doing things. Or maybe it’s just because I’m not used to these APIs 😄
Going through the code inside of
OnWindowCreated we can see how we first set the
ExtendsContentIntoTitleBar to false so we really make the titlebar disappear. Then, there are a few lines to get a so-called handle, the native pointer to a window on Windows, to eventually get to the
AppWindow object which is a native Windows object. This is the representation of a window on Windows, using WinUI.
AppWindow has a
Presenter, right now through .NET MAUI, we always set that presenter to the
OverlappedPresenter. That is why there is only one option in the switch statement. This presenter determines the look and feel of the window.
To make our window fullscreen, we set
SetBorderAndTitleBar both to false and then call
Maximize to maximize the window. And that’s it!
Enable/disable maximize and minimize buttons #
There is much more you can do with a window, even beyond enabling or disabling the maximize and minimize buttons. You can remove the border of a window, prevent the window from being resized with a mouse cursor and some other things.
If you want to disable the maximize button, simply add this line in that switch statement:
overlappedPresenter.IsMaximizable = false;
I assume you can guess what you should do to enable it again or do the same for the minimize button 😉
Trigger Programmatically or Through User Interaction #
All that is described above, does so while the app is starting up and sets your window in a certain state. While that is great, you might find yourself unable to close the app if you’re trying this out. When the app is in fullscreen, you can no longer click the close button.
That’s why you might want to have a button that toggles fullscreen or some other form of (user) interaction that influences this behavior.
With some modifications you can also use this in a
Button tap for instance. Have a look at the code below.
Toggle fullscreen, minimize and maximize buttons in .NET MAUI Windows through event handlers
In this code you find a private method called
GetAppWindow. This returns the Windows native
AppWindow object based on the .NET MAUI
Window object that you provide.
Underneath that, you can see 3 event handlers that each do something. One toggles the fullscreen functionality, one toggles the maximize button and one toggles the minimize button.
While this is implemented through the tap of a button, this can of course be done by any event or command in your application. Below you can see a screenshot of the app where this code originates from, the maximize button is currently disabled.
Wrap up #
That’s it! Adding this functionality is not necessarily rocket science, but still if you’re not familiar with Windows and WinUI APIs it might not be straight-forward either. And now you know how to do it!
You can find the full code and running sample on the GitHub repository for this: https://github.com/jfversluis/MauiWindowsFullscreenMinimizeMaximizeSample