代码之家  ›  专栏  ›  技术社区  ›  Mikael Dúi Bolinder bielawski

如何使UWP AppWindow进入全屏模式?

  •  0
  • Mikael Dúi Bolinder bielawski  · 技术社区  · 4 年前

    我在用 AppWindow 为应用程序创建多个窗口,我希望用户能够使窗口全屏显示,但是 ApplicationView.TryEnterFullScreenMode 不起作用,在使用时始终返回false 应用程序窗口 .

    Sample code 来自Microsoft文档:

    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
        }
    }
    

    你怎么做 应用程序窗口 进入全屏模式?

    0 回复  |  直到 4 年前
        1
  •  0
  •   Mikael Dúi Bolinder bielawski    4 年前

    For 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();
        }
    }
    
    推荐文章