Reputation: 4789
how to determine when navigationwindow back button is pressed and trap that event to something extra. I am thinking of managing the page state.
Upvotes: 1
Views: 3366
Reputation: 7517
Add a handler to either NavigationWindow.Navigating
or NavigationService.Navigating
. In your handler:
void NavigationService_Navigating(object sender, NavigatingCancelEventArgs e)
{
if (e.NavigationMode == NavigationMode.Back) {
e.Cancel = true;
// TODO: whatever state management you're going to do
}
}
P.s. You will need to register the navigation service. In my code it didn't work on the page constructor because the navigation service was still null. So I added Loaded="page_Loaded" to the XAML page tag and assigned it there:
bool _navigationServiceAssigned = false;
private void page_Loaded(object sender, RoutedEventArgs e)
{
if (_navigationServiceAssigned == false)
{
NavigationService.Navigating += NavigationService_Navigating;
_navigationServiceAssigned = true;
}
}
The NavigatingCancelEventArgs
contains all of the information about the navigation request you'll need to manage page state.
Upvotes: 4
Reputation: 1685
The NavigationService
provides a number of events you can subscribe to, if you want to control the navigation process:
Upvotes: 1