1. Navigation Using the NavigationService
The PhoneApplicationPage
class exposes a public NavigationService
property, which allows direct control over navigation.
Note
The NavigationService
cannot be used to launch Internet Explorer to view an external URL. Instead, use either the WebBrowserTask
, or open the page within the app using the WebBrowserControl
. See the previous sections on external navigation using the Button
and HyperlinkButton
controls.
The NavigationService.Navigate
method causes the frame to load the specified PhoneApplicationPage
, like so:
NavigationService.Navigate(
new Uri("/DirectoryName/PageName.xaml", UriKind.Relative));
The URI must be either a path relative to the
project’s root directory, as shown in the previous example, or a
relative component URI, such as in the following example:
NavigationService.Navigate(
new Uri("/AssemblyName;component/PageName.xaml", UriKind.Relative));
Tip
The NavigationService
cannot be used within the constructor of the PhoneApplicationPage
because it is assigned after the page’s constructor has been called. Therefore, wait until the page’s OnNavigatedTo
method is called or the Loaded
event has occurred before using the NavigationService
.
The NavigationService.Source
property allows you to retrieve the URI of the current page. Setting the Source
property performs the same action as using the Navigate
method; the frame loads the page at the specified URI. See the following example:
NavigationService.Source = new Uri(
"/DirectoryName/PageName.xaml", UriKind.Relative);
Routing is also enabled for the NavigationService
, which means that mapped URIs can be used instead of relative URIs.
If you examine the API of the NavigationService
, you will likely wonder what the difference is between the CurrentSource
property and the Source
property. The answer is that the CurrentSource
property does not change until navigation has completed. Conversely, the Source
property changes as soon as navigation is initiated.
Backward Navigation
The NavigationService
maintains the app’s navigation history, via an internal Journal
instance. This allows the GoBack
method of the NavigationService
to move to the previous page in the history stack.
Note
If the GoBack
method is called, and the history stack is empty because the current page is the app’s start page, then an InvalidOperationException
is raised. To determine whether the NavigationService
is able to go back, query its CanGoBack
property.
Forward Navigation
Unlike Silverlight for the browser, the GoForward
method of the NavigationService
does not allow forward navigation and will raise an InvalidOperationException
when called. Consequently, the CanGoForward
property always returns false
.
Note
Forward navigation using the NavigationService
is not supported.
2. Handling Page Navigation
The PhoneApplicationPage
extends the System.Windows.Controls.Page
class, which has a number of virtual methods called when the page is brought into view or removed from view by the PhoneApplicationFrame
(see Figure 1).
FIGURE 1 PhoneApplicationPage
inherits page navigation methods.
The OnNavigatingFrom
method is called before a page is removed from view by the PhoneApplicationFrame
, and the OnNavigatedFrom
method is called after navigation occurs. Conversely, the OnNavigatedTo
method is called when the frame brings the page into view.