【问题标题】:How to make UWP AppWindow enter full screen mode?如何让 UWP AppWindow 进入全屏模式?
【发布时间】:2021-04-20 14:12:12
【问题描述】:

我正在使用AppWindow为应用程序创建多个窗口,我希望用户能够使窗口全屏,但ApplicationView.TryEnterFullScreenMode不起作用,使用时它一直返回false在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 进入全屏模式?

【问题讨论】:

    标签: c# uwp fullscreen uwp-appwindow


    【解决方案1】:

    对于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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-29
      • 2021-01-20
      相关资源
      最近更新 更多