Created
February 13, 2023 13:03
-
-
Save jfversluis/d42e9a0c5c3e88869898189e29c08977 to your computer and use it in GitHub Desktop.
Toggle fullscreen, minimize and maximize buttons in .NET MAUI Windows through event handlers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if WINDOWS | |
private Microsoft.UI.Windowing.AppWindow GetAppWindow(MauiWinUIWindow window) | |
{ | |
var handle = WinRT.Interop.WindowNative.GetWindowHandle(window); | |
var id = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(handle); | |
var appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(id); | |
return appWindow; | |
} | |
#endif | |
private void OnToggleFullscreenClicked(object sender, EventArgs e) | |
{ | |
#if WINDOWS | |
var window = GetParentWindow().Handler.PlatformView as MauiWinUIWindow; | |
var appWindow = GetAppWindow(window); | |
switch (appWindow.Presenter) | |
{ | |
case Microsoft.UI.Windowing.OverlappedPresenter overlappedPresenter: | |
if (overlappedPresenter.State == Microsoft.UI.Windowing.OverlappedPresenterState.Maximized) | |
{ | |
overlappedPresenter.SetBorderAndTitleBar(true, true); | |
overlappedPresenter.Restore(); | |
} | |
else | |
{ | |
overlappedPresenter.SetBorderAndTitleBar(false, false); | |
overlappedPresenter.Maximize(); | |
} | |
break; | |
} | |
#endif | |
} | |
private void OnToggleMaximizeClicked(object sender, EventArgs e) | |
{ | |
#if WINDOWS | |
var window = GetParentWindow().Handler.PlatformView as MauiWinUIWindow; | |
var appWindow = GetAppWindow(window); | |
switch (appWindow.Presenter) | |
{ | |
case Microsoft.UI.Windowing.OverlappedPresenter overlappedPresenter: | |
overlappedPresenter.IsMaximizable = !overlappedPresenter.IsMaximizable; | |
break; | |
} | |
#endif | |
} | |
private void OnToggleMinimizeClicked(object sender, EventArgs e) | |
{ | |
#if WINDOWS | |
var window = GetParentWindow().Handler.PlatformView as MauiWinUIWindow; | |
var appWindow = GetAppWindow(window); | |
switch (appWindow.Presenter) | |
{ | |
case Microsoft.UI.Windowing.OverlappedPresenter overlappedPresenter: | |
overlappedPresenter.IsMinimizable = !overlappedPresenter.IsMinimizable; | |
break; | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment