带消息对话框的Uwp条件返回导航



我想问用户他们是否确定他们想要向后移动,并且只有当他们选择"是"时才向后移动,为此我使用了一个消息对话框,但问题是当我按下后退按钮时它会向后移动,然后弹出对话框询问我是否想要向后移动。

我检查了断点,只要messageDialog。ShowAsync执行页移回。

protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
SystemNavigationManager.GetForCurrentView().BackRequested += SampleConditionalNavigation_BackRequested;
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;
SystemNavigationManager.GetForCurrentView().BackRequested -= SampleConditionalNavigation_BackRequested;
}
private async void SampleConditionalNavigation_BackRequested(object sender, BackRequestedEventArgs e)
{
e.Handled = true; // I have also tried setting it to false
var messageDialog = new MessageDialog("Are you sure you want to move back?") { Title = "Confirmation" };
// Add commands and set their callbacks; both buttons use the same callback function instead of inline event handlers
messageDialog.Commands.Add(new UICommand("Yes"));
messageDialog.Commands.Add(new UICommand("No"));
// Set the command that will be invoked by default
messageDialog.DefaultCommandIndex = 0;
// Set the command to be invoked when escape is pressed
messageDialog.CancelCommandIndex = 1;
//ShowDialog
var result = await messageDialog.ShowAsync();
if (result.Label == "Yes")
{
var rootFrame = Window.Current.Content as Frame;
if (rootFrame?.CanGoBack is true)
{
rootFrame?.GoBack();
}
}
}

更新1

即使我注释了对话框显示自己的代码,页面仍然导航回来,似乎e。已处理= true在这里几乎没什么用

这段代码是绝对正确的,工作完美,问题是我的应用程序有BackRequested在app.xaml.cs中的事件那个事件在这个页面上的这个方法能够执行之前已经在执行并导航回来了。把它从app. example .cs中移除修复了这个问题。

相关内容

  • 没有找到相关文章

最新更新