The OnNavigatingFrom
method offers the opportunity to cancel navigation using the NavigatingCancelEventArgs
parameter.
NavigatingCancelEventArgs
has the following properties:
NavigationMode—An enum value that indicates the type of navigation. This value may be Back, Forward, New, or Refresh.
Uri—The destination URI.
Cancel—Setting this value to true cancels the navigation.
The NavigationCancelEventArgs
class subclasses CancelEventArgs
, which provides the Cancel
property. By setting this property to true
, the page can prevent the navigation from occurring, as shown in the following excerpt:
protected override void OnNavigatingFrom(
System.Windows.Navigation.NavigatingCancelEventArgs e)
{
base.OnNavigatingFrom(e);
MessageBoxResult boxResult = MessageBox.Show(
"Leave this page?", "Question", MessageBoxButton.OKCancel);
if (boxResult != MessageBoxResult.OK)
{
e.Cancel = true;
}
}
Note
You should not attempt to cancel navigation in the OnNavigatingFrom
method when the hardware Back button is pressed. Instead, override the OnBackKeyPress
method to cancel the back key, which prevents navigation.
18.191.139.42