GoBack 被调用 3 次 uwp

GoBack get called 3times uwp

如果我按下 phone 上的后退按钮,GoBack 方法将被调用 3 次,然后我转到上一页的开始页面(但它工作 1 次,共 20 次)。但是在 PC 上它每次只需要一个调用就可以工作并且总是到达上一页。

这是class中的startMethod:

 public DetailPageFavorites()
        {
       this.InitializeComponent();
        // If on a phone device that has hardware buttons then we hide the app's back button.
        if (ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
        {
            this.BackButton.Visibility = Visibility.Collapsed;
            this.button_like.Margin = new Thickness(0, 0, 0, 0);
        }
        SystemNavigationManager.GetForCurrentView().BackRequested += SystemNavigationManager_BackRequested;
}

按下硬件按钮时使用的方法:

   private void SystemNavigationManager_BackRequested(object sender, BackRequestedEventArgs e)
    {
        Frame frame = Window.Current.Content as Frame;
        e.Handled = true;
        if (frame.CanGoBack)
        {
            frame.GoBack();
        }

    }

PC端使用的方法:

 private void BackButton_Click(object sender, RoutedEventArgs e)
        {
            GoBack();
        }
        private void GoBack()
        {
            Frame frame = Window.Current.Content as Frame;
            if (frame == null)
            {
                return;
            }
            if (frame.CanGoBack)
            {
                frame.GoBack();
            }
        }

确保在导航到其他页面之前删除 SystemNavigationManager.GetForCurrentView().BackRequested 事件处理程序。

Page.Unloaded事件或OnNavigatedFrom方法中。

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
            base.OnNavigatedFrom(e);
            SystemNavigationManager.GetForCurrentView().BackRequested -= SystemNavigationManager_BackRequested;
}