【问题标题】:How to handled HardwareButtons.BackPressed in UWP?如何处理硬件按钮。在 UWP 中按下后退?
【发布时间】:2016-04-14 14:31:41
【问题描述】:

我有带有 Prism 和 AppShell 的 UWP 应用程序。 我想在 BackButton 退出之前添加确认对话框。 我试过这个:

protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args)
    {
        ...
        SystemNavigationManager.GetForCurrentView().BackRequested += App_BackRequested;

        ...
    }

private void App_BackRequested(object sender, BackRequestedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;
        if (rootFrame == null)
        {  
           return;
        }

        if (rootFrame.CanGoBack && e.Handled == false)
        {
            <add confirm dialog here>
            e.Handled = true;
        }
    }

但 rootFrame 始终为空,如果历史堆栈为空并且我按下 BackButton 应用程序关闭,即使 imake 如下:

private void App_BackRequested(object sender, BackRequestedEventArgs e)
{
        e.Handled = true;
}

我也试过了

HardwareButtons.BackPressed += App_BackRequested;

它也无济于事。

【问题讨论】:

    标签: c# xaml win-universal-app prism


    【解决方案1】:

    在 App.xaml.cs 中添加代码 -

     private async void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
        {
    
            e.Handled = true;
            Frame rootFrame = Window.Current.Content as Frame;
            if (rootFrame.CanGoBack && rootFrame != null)
            {
    
                rootFrame.GoBack();
            }
            else
            {
                var msg = new MessageDialog("Confirm Close");
                var okBtn = new UICommand("OK");
                var cancelBtn = new UICommand("Cancel");
                msg.Commands.Add(okBtn);
                msg.Commands.Add(cancelBtn);
                IUICommand result = await msg.ShowAsync();
    
                if (result != null && result.Label == "OK")
                {
                    Application.Current.Exit();
                }
            }
        }
    

    在 App.xaml.cs 的构造函数中添加这一行 -

    HardwareButtons.BackPressed += HardwareButtons_BackPressed;
    

    【讨论】:

    • 它适用于新的空白项目,但不适用于我的应用程序。 rootFrame 始终为空,如果我在弹出应用程序关闭时按 NO。我认为是因为我有这个:protected override UIElement CreateShell(Frame rootFrame) { var shell = Container.Resolve(); shell.SetContentFrame(rootFrame);返回外壳; }
    【解决方案2】:

    尝试将以下这些行添加到 OnLaunchApplicationAsync 方法

    protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args)
        {
            //......
            DeviceGestureService.GoBackRequested += (s, e) =>
            {
                e.Handled = true;
                e.Cancel = true;
                if (NavigationService.CanGoBack())
                {
                    NavigationService.GoBack();
                }
                else
                {
                    // PUT YOUR LOGIC HERE (Confirmation dialog before exit)
                    Application.Current.Exit();
                }
            };
            //......
         }
    

    【讨论】:

    • +1。如果你解释你通过这个实现的目标,那么 PO 就更容易理解你是如何解决问题的。
    • 使用 Prism 可以处理硬件按钮事件,例如 (CameraButtonReleased, CameraButtonPressed, CameraButtonHalfPressed, GoBackRequested, .....) 只需在 OnLaunchApplicationAsync 方法中实现即可
    猜你喜欢
    • 2016-02-07
    • 2011-04-02
    • 2017-01-26
    • 1970-01-01
    • 2015-04-30
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多