Andrej Tozon's blog

In the Attic

NAVIGATION - SEARCH

Countdown to Silverlight 3 #7: Navigation

Another powerful Silverlight 3 feature is navigation. The concept is very simple: provide a space on your main page, which you’ll later fill with custom content – pages. You’ll be able to navigate through these pages (by using browser’s Forward/Back buttons, directly access them and even provide custom parameters.

There’s a special project template in Visual Studio 2008 to create a Silverlight Navigation application and it provides a good start if you’re new with this feature – you’ll get a nice template to get you going, which you’ll be able to customize as you go forward.

There is one core control that you’ll need to enable this feature – System.Windows.Controls.Frame, which will host your pages by navigating to them. A Page is a special control, derived from UserControl, adding navigation capabilities.

I refactored my Silverlight 3 features application to a Navigation application, each sample being a separate page, accessible from a list of samples. I’ll continue to add new samples to this application in the future. I’ll try to maintain the main application (as well as the examples pages) to be as much MVVM-ish as possible, but without having to resort to any external dependencies.

A couple of things to note in the new, refactored application:

1. By using Element binding feature to bind the Frame Source to selected Url in the samples list, calls to Navigate() method aren’t required – binding does all the magic.

...
<ListBox x:Name="views"
           ItemsSource="{Binding Links, Source={StaticResource viewModel}}" ... />
...
<navigation:Frame Source="{Binding SelectedItem.Value, ElementName=views}" ... />
...

2. Passing custom parameters to a page. MergedResourceDictionaries page has been modified so it accepts a parameter of a theme to be applied to the sample form.
Example: http://tozon.info/sl3/#/Views/MergedResourceDictionaries.Xaml?style=Red

3. Uri mapping. Parameters for MergedResourceDictionaries can be passed in to the paged in a shorter form, thanks to the Uri mapper.
Examples: http://tozon.info/sl3/#Form/Red| http://tozon.info/sl3/#Form/Blue

4. To prevent any mis-addressing of navigation pages, attach a handler to frame’s NavigationFailed event and handle those cases to spare your users from seeing some nasty popup windows, telling them they misspelled the url.

private void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
{
e.Handled = true;
views.SelectedIndex = 0;
}

Silverlight 3 navigation feature greatly simplifies creation of modern, interactive web sites with multiple pages, where previous to SL3, ASP.NET was in lots of cases the first choice.

Additional reading
http://timheuer.com/blog/archive/2009/03/22/silverlight-navigation-framework-and-uri-routing.aspx

http://blogs.microsoft.co.il/blogs/alex_golesh/archive/2009/04/02/
silverlight-3-quick-tip-6-navigation-framework-and-uri-routing.aspx

Run the sample online

Source code below: