I'm using AppWindow
to create multiple windows for the app, and I want the user to be able to make the windows full screen, but ApplicationView.TryEnterFullScreenMode
doesn't work, it returns false all the time when used in an AppWindow
.
Sample code from Microsoft Docs:
private void ToggleFullScreenModeButton_Click(object sender, RoutedEventArgs e)
{
var view = ApplicationView.GetForCurrentView();
if (view.IsFullScreenMode)
{
view.ExitFullScreenMode();
}
else
{
view.TryEnterFullScreenMode(); // Returns false in an AppWindow
}
}
How do you make an AppWindow
enter full screen mode?
For AppWindow
you should use AppWindowPresenter.RequestPresentation
and pass AppWindowPresentationKind.FullScreen
as parameter.
The solution I came up with (based on this answer) handles exiting full screen mode using:
XAML:
<Button x:Name="ToggleFullScreenButton" Text="Full screen mode" Click="ToggleFullScreenButton_Click" />
Code behind:
public AppWindow AppWindow { get; } // This should be set to the AppWindow instance.
private void ToggleFullScreenButton_Click(object sender, RoutedEventArgs e)
{
var configuration = AppWindow.Presenter.GetConfiguration();
if (FullScreenButton.Text == "Exit full screen mode" && _tryExitFullScreen())
{
// Nothing, _tryExitFullScreen() worked.
}
else if (AppWindow.Presenter.RequestPresentation(AppWindowPresentationKind.FullScreen))
{
FullScreenButton.Text = "Exit full screen mode";
_ = Task.Run(async () =>
{
await Task.Delay(500); // Delay a bit as AppWindow.Changed gets fired many times on entering full screen mode.
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
AppWindow.Changed += AppWindow_Changed;
this.KeyDown += Page_KeyDown;
});
});
}
}
private bool _tryExitFullScreen()
{
if (AppWindow.Presenter.RequestPresentation(AppWindowPresentationKind.Default))
{
FullScreenButton.Text = "Full screen mode";
AppWindow.Changed -= AppWindow_Changed;
this.KeyDown -= Page_KeyDown;
return true;
}
return false;
}
// handles the back-to-window button in the title bar
private void AppWindow_Changed(AppWindow sender, AppWindowChangedEventArgs args)
{
if (args.DidSizeChange) // DidSizeChange seems good enough for this
{
_tryExitFullScreen();
}
}
// To make the escape key exit full screen mode.
private void Page_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Escape)
{
_tryExitFullScreen();
}
}