GoBack被称为3次uwp

时间:2016-01-05 15:14:26

标签: c# windows-phone uwp

如果我按下手机上的后退按钮,GoBack方法会被调用3次,然后我会进入上一页的启动页面(但它可以工作1次,每次20次)。但是在PC上它每次只有一个电话就可以工作,并且总是到达上一页。

这是类中的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;
}

如果出现hardwareButton则使用的方法:

   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();
            }
        }

1 个答案:

答案 0 :(得分:2)

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

Page.Unloaded事件或OnNavigatedFrom方法。

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