【问题标题】:(UWP) Hardware Back Press work correctly in mobile but error with PC(UWP) 硬件 Back Press 在移动设备上正常工作,但在 PC 上出现错误
【发布时间】:2017-01-08 19:55:30
【问题描述】:

在我的 UWP 应用程序中,当我单击移动后退按钮应用程序关闭时,请将此代码添加到 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! \nOr Press Refresh Button to Go Back");
               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();
               }
           }
       }

    public App()
    {            
        this.InitializeComponent();
        this.Suspending += OnSuspending;

    /*  Because of this line my app work on mobile great but when
        when i debug on pc it through exception "show in image" */
        HardwareButtons.BackPressed += HardwareButtons_BackPressed;
    }

当我在手机上调试应用程序时完成所有这些代码后,应用程序成功运行 - 移动调试:

但是当使用相同的代码在 pc 中调试时,它会显示此错误-PC 调试:

当我删除 HardwareButtons.BackPressed += HardwareButtons_BackPressed; 时,PC 调试问题已解决,但在移动调试中,返回按钮再次不起作用。

【问题讨论】:

  • 你可以试试SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested; SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;而不是`HardwareButtons.BackPressed'
  • 我的问题还是一样,它在移动和 PC 中再次通过异常这是我的 app.xaml.cs 文件的链接,如果你可以帮助我1drv.ms/u/s!Ahppl6XCmlXfj_VUrekI819VqZLG-Q

标签: c# win-universal-app uwp windows-10-mobile mobile-development


【解决方案1】:

原因是HardwareButtons API 不是处理后退按钮的通用解决方案。此 API 仅在移动扩展 SDK 中可用,尝试在其他 SKU 上调用它会导致此异常,因为该类型不可用。

要在所有系统上启用相同的功能,您需要使用新的通用后退按钮事件:

SystemNavigationManager.GetForCurrentView().BackRequested += BackButtonHandler;

这在手机、PC、平板电脑、Xbox One、Surface Hub 和 HoloLens 上同样适用。

在 PC 上,此按钮默认不显示,因此您必须手动显示或创建自己的按钮。要在窗口的标题栏中显示“后退”按钮,请使用:

SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
   AppViewBackButtonVisibility.Visible;

建议您在 Frame.CanGoBack 为 false 时隐藏此按钮,因为在这种情况下该按钮不再有用。您应该在框架的每次导航之后执行此操作。最好的地方是在App.xaml.cs 中设置根框架时:

 Frame rootFrame = Window.Current.Content as Frame;
 rootFrame.Navigated += UpdateAppViewBackButton;

现在处理程序可能如下所示:

private void UpdateAppViewBackButton( object sender, NavigationEventArgs e )
{
    Frame frame = (Frame) sender;
    var systemNavigationManager = SystemNavigationManager.GetForCurrentView();
    systemNavigationManager.AppViewBackButtonVisibility =
        frame.CanGoBack ? AppViewBackButtonVisibility.Visible : 
                          AppViewBackButtonVisibility.Collapsed;
}

在应用程序关闭时

我还注意到您正在使用Application.Current.Exit(); 退出应用程序。但是,不建议这样做。一旦用户在对话框中选择 OK,您应该设置 e.Handled = false 并让系统手动处理应用程序关闭。这将确保应用程序暂停将按预期运行,并且如果系统有足够的资源,应用程序将保留在内存中,然后将再次更快地启动。 Application.Current.Exit() 杀死应用程序,不推荐用于 UWP 应用程序。

要记住的一点是,在桌面上,目前无法捕捉用户单击应用标题栏中的关闭按钮,因此很遗憾在这种情况下不会显示您的确认对话框。

【讨论】:

  • 我的问题还是一样,它在移动和 PC 中再次出现异常,这是我的 app.xaml.cs 文件的链接,如果你可以帮助我1drv.ms/u/s!Ahppl6XCmlXfj_VUrekI819VqZLG-Q
  • 我已将您提供的代码复制并粘贴到 VS 中,PC 和手机上都没有崩溃。什么时候使用 UpdateAppViewBackButton 方法?它似乎没有在代码中的任何地方引用。
  • 应用程序究竟什么时候崩溃(有什么例外,在哪一行)?
  • 如果您尝试将提供的代码复制并粘贴到一个空的 UWP 项目中,它似乎也可以正常工作,所以我怀疑问题可能出在其他地方。
  • 感谢感谢,现在我背面的消息框看起来也很棒!
猜你喜欢
  • 1970-01-01
  • 2011-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多