如何解决如何让 UWP AppWindow 进入全屏模式?
我正在使用 AppWindow
为应用程序创建多个窗口,并且我希望用户能够使窗口全屏显示,但是 ApplicationView.TryEnterFullScreenMode
不起作用,它返回 false all在 AppWindow
中使用的时间。
Sample code 来自 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
}
}
如何让 AppWindow
进入全屏模式?
解决方法
对于 AppWindow
,您应该使用 AppWindowPresenter.RequestPresentation
并将 AppWindowPresentationKind.FullScreen
作为参数传递。
我提出的解决方案(基于 this answer)使用以下方法处理退出全屏模式:
- 原始按钮。
- 标题栏中的返回窗口按钮。
- 退出键。
XAML:
<Button x:Name="ToggleFullScreenButton" Text="Full screen mode" Click="ToggleFullScreenButton_Click" />
背后的代码:
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();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。