The NavigationService allows a developer to navigate to other pages inside a Windows Phone 7 application using view models only.

The service will automatically determine all pages inside the application domain that implement the PhoneApplicationPage<TViewModel>. Other windows will have to be registered manually or following the right naming convention.

Available for
WPF / Silverlight / WP7 / WP7 Mango

Test/Emulation service available: yes

For WPF and Silverlight, the pages must inherit from Page<TViewModel>. For WP7, the pages must inherit from PhoneApplicationPage<TViewModel>.

In WPF, the parameters are of type Dictionary<string, object>, in Silverlight and WP7, the arguments are of type Dictionary<string, string>.


Navigating to a new view
To navigate to a new page, use the following code:

var navigationService = GetService<INavigationService>();
navigationService.Navigate<EmployeeViewModel>();

Navigating with parameters
It is very easy to navigate to a new page with parameters. Use the following code:

var parameters= new Dictionary<string, object>();
parameters.Add("id", employee.EmployeeID);

var navigationService = GetService<INavigationService>();
navigationService.Navigate<EmployeeViewModel>(parameters);

Navigating back and forward
The service also supports navigating back and forward:

var navigationService = GetService<INavigationService>();
navigationService.GoBack(); // navigates to the previous page, obviously
navigationService.GoForward(); // navigates to the next page, obviously

Navigating to a custom Uri
To navigate to a custom uri without a view model type, use the following code. Of course it's also possible to pass parameters using the right method overload.

var navigationService = GetService<INavigationService>();
navigationService.Navigate("/UI/Pages/EmployeePage.xaml");

Registering custom views
To register custom views, use the following code:

var navigationService = GetService<INavigationService>();
navigationService.Register(typeof(EmployeeViewModel), typeof(EmployeeDetailsPage));

Last edited Jul 18, 2011 at 3:51 PM by GeertvanHorrik, version 3

Comments

No comments yet.