处理后退按钮Windows 8.1 Universal app

时间:2014-07-03 15:25:27

标签: windows-runtime windows-phone-8.1 win-universal-app

我将我的应用程序从Windows Phone 8 Silverlight更新为Windows 8.1 RT(我认为称之为)。

我刚刚创建了第二页,当我转到并按下后退按钮时,它会退出我的应用,而不是返回第一页。

我不知道为什么会发生这种情况,默认行为会在最后一页出现?

我无法找到如何覆盖后退按钮事件以进行Frame.GoBack()来电。

这是开发预览错误还是我错过了什么?

2 个答案:

答案 0 :(得分:5)

放入第二页的构造函数:(SecondPage.xaml.cs)

Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;

然后定义eventhandler函数:

    private void HardwareButtons_BackPressed( object sender, BackPressedEventArgs e )
    {
        Frame.GoBack();
        e.Handled = true;
    }

答案 1 :(得分:-1)

在通用Windows应用程序中,您还可以处理"后退按钮"在App.xaml.cs文件中全局单击。请参阅以下内容:

  1. 在OnLaunched方法中添加以下代码:

    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
      //..Rest of code...
    
       rootFrame.Navigated += OnNavigated;
    
    
        // Register a handler for BackRequested events and set the
        // visibility of the Back button:
    
        SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
    
        SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
            rootFrame.CanGoBack  ?
            AppViewBackButtonVisibility.Visible :
            AppViewBackButtonVisibility.Collapsed;
    
        //..Rest of code...
    
    }
    
  2. 然后你应该在App类中添加处理程序方法的代码:

        private void OnNavigated(object sender, NavigationEventArgs e)
        {
          // Each time a navigation event occurs, update the Back button's visibility:
    
            SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
            ((Frame)sender).CanGoBack ?
            AppViewBackButtonVisibility.Visible :
            AppViewBackButtonVisibility.Collapsed;
        }
    
    
    
       private void OnBackRequested(object sender, BackRequestedEventArgs e)
       {
          Frame rootFrame = Window.Current.Content as Frame;
    
          if (rootFrame.CanGoBack)
          {
            e.Handled = true;
            rootFrame.GoBack();
          }
       }