1

当用户在点击后退按钮后点击“取消”按钮时,如何阻止我的应用程序返回?

      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
        }
    }
4

2 回答 2

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;
    }
}
于 2012-06-16T01:04:01.380 回答
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;
        }
    }
于 2013-10-18T17:29:33.420 回答