1. Page Redirection
The OnNavigatingFrom
method allows you to intercept a navigation event and to even cancel
the navigation if needed. Additionally, there may be times when you
want to redirect the user to a different URI based on some conditional
logic.
The NavigationService
,
however, does not support overlapping navigation. That is, you are
unable to cancel an existing navigation and immediately commence
another.
You can, however, cancel navigation and schedule navigation to a different Uri
using the page’s Dispatcher
property, as shown in the following excerpt:
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
if (e.Uri.ToString().Contains("RequestedUrl"))
{
e.Cancel = true;
/* Perform the redirect on the UI thread. */
Dispatcher.BeginInvoke(() => NavigationService.Navigate(
new Uri("RedirectUrl", UriKind.Relative)));
}
base.OnNavigatingFrom(e);
}
By using the Dispatcher
to invoke the lambda expression, which performs the call to the NavigationService
,
you allow the current navigation to complete first. This works because
the UI thread can be thought of as a queue of prioritized delegates,
all waiting in turn to be executed. After all the Navigating
event handlers have been serviced, the delegate represented by the
lambda expression will be taken of the queue and performed by the UI
thread. This technique, of using the Dispatcher
to enqueue an action, is also useful when working with some UI
controls, whose event handlers may be called before the control is
finished reacting to a user action.
2. Hardware Back Button
The hardware Back button is analogous to the
Back button on a web browser. However, when the user presses the Back
button, past the first page of a phone app, the app is closed. This is
in contrast to the phone’s hardware start button, which merely causes
an app to be deactivated.
Note
The hardware Back button should not be used
for application-specific behavior. It is only for navigation, and if
used otherwise, may cause your app to fail Windows Phone Marketplace
certification.
To determine whether navigation is occurring
because the hardware Back button was pressed or the navigation was
initiated by a call to NavigationService.GoBack
, use the NavigatingEventArgs.NavigationMode
property as shown:
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
if (e.NavigationMode == NavigationMode.Back)
{
// Back button pressed.
}
}
The back key button can also be canceled by overriding the PhoneApplicationPage.OnBackKeyPress
, as shown in the following excerpt:
protected override void OnBackKeyPress(CancelEventArgs e)
{
base.OnBackKeyPress(e);
e.Cancel = true;
}
OnBackKeyPress
is called before OnNavigatedFrom
, and if the Back button is canceled, OnNavigatedFrom
is not called at all.
Note
The Windows Phone Marketplace certification
requirements forbid canceling the Back button in most cases. To
maintain a consistent user experience, the Back button must only be
used for backward navigation in the application. The following four
certification requirements relate to use of the Back button:
- Pressing the Back button must return the application to the previous page or return to any previous page within the back stack.
- Pressing the Back button from the first screen of an application must close the application.
- If the current page displays a context menu or a dialog, the pressing
of the Back button must close the menu or dialog and return the user to
the screen where the context menu or dialog box was opened.
- For games, when the Back button is pressed during gameplay, the game
can choose to present a pause context menu or dialog or navigate the
user to the prior menu screen. Pressing the Back button again while in
a paused context menu or dialog closes the menu or dialog.