WP7停止页面从导航/返回

时间:2012-06-16 01:00:39

标签: windows-phone-7

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

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

2 个答案:

答案 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;
        }
    }