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 に答える