C# GoBack被呼叫3次uwp
如果我按下手机上的Back按钮,GoBack方法会被调用3次,我会转到起始页而不是上一页(但它可以工作20次中的1次)。但是在PC上,每次只需一个电话,它就可以工作,并且总是能进入上一页 这是课堂上的基本方法:C# GoBack被呼叫3次uwp,c#,windows-phone,uwp,C#,Windows Phone,Uwp,如果我按下手机上的Back按钮,GoBack方法会被调用3次,我会转到起始页而不是上一页(但它可以工作20次中的1次)。但是在PC上,每次只需一个电话,它就可以工作,并且总是能进入上一页 这是课堂上的基本方法: public DetailPageFavorites() { this.InitializeComponent(); // If on a phone device that has hardware buttons then we hid
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.unload
事件或OnNavigatedFrom
方法中
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
SystemNavigationManager.GetForCurrentView().BackRequested -= SystemNavigationManager_BackRequested;
}