当用户点击后退按钮后单击“取消”按钮时,如何阻止我的应用程序返回?
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
var buttonInfo = MessageBox.Show("Are you sure you want to exit?", "Exit", MessageBoxButton.OKCancel);
if (buttonInfo == MessageBoxResult.OK)
{
this.NavigationService.GoBack();
}
else
{
//How to stop page from navigating
}
}
答案 0 :(得分:2)
使用CancelEventArgs
取消操作,属性Cancel。
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
// If the event has already been cancelled, do nothing
if(e.Cancel)
return;
var buttonInfo = MessageBox.Show("Are you sure you want to exit?", "Exit", MessageBoxButton.OKCancel);
if (buttonInfo == MessageBoxResult.OK)
{
this.NavigationService.GoBack();
}
else
{
//Stop page from navigating
e.Cancel = true;
}
}
答案 1 :(得分:1)
多一点..
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
if (e.Cancel)
return;
var buttonInfo = MessageBox.Show("Are you sure you want to exit?", "Exit", MessageBoxButton.OKCancel);
if (buttonInfo == MessageBoxResult.OK)
{
**//this line may useful if you are in the very first page of your app**
if (this.NavigationService.CanGoBack)
{
this.NavigationService.GoBack();
}
}
else
{
//Stop page from navigating
e.Cancel = true;
}
}