Monday, January 16, 2012

Silverlight: Navigation Framework tip: using the XAML escape sequence

With all the Windows 8/Metro/WinRT hype on the internet today, I decided it was time for a quick tip concerning the XAML escape sequence and the good old navigation framework present in Silverlight 4 and Silverlight 5. I only learned about the XAML escape sequence until recently and I can image that there quite a number of people out there that are still unfamiliar with it. The nice thing is that you can also use the XAML escape sequence in WPF, Windows Phone and WinRT, if you like to do so.
The problem

When you use the navigation framework in Silverlight, you probably also want to use an UriMapper, to shield your users from the complete path of the XAML files and the .xaml extension. Such an UriMapper could look something like this:

<uriMapper:UriMapper>

<uriMapper:UriMapping Uri="Home" MappedUri="/Views/Home.xaml"/>
<uriMapper:UriMapping Uri="About" MappedUri="/Views/About.xaml"/>
</uriMapper:UriMapper>

A small bug in the navigation framework activates the browser’s back button when the user navigates to your Silverlight application for the very first time. This isn’t that bad, except for the exception when the user actually presses the back button:

image_thumb.png

A well known workaround for this problem is to change your UriMapper like this:

<uriMapper:UriMapper>

<uriMapper:UriMapping Uri="" MappedUri="/Views/Home.xaml"/>
<uriMapper:UriMapping Uri="Home" MappedUri="/Views/Home.xaml"/>
<uriMapper:UriMapping Uri="About" MappedUri="/Views/About.xaml"/>
</uriMapper:UriMapper>

Which causes your application to navigate when the entered uri is empty, which happens when the user presses the back button of the browser when he first visits your application. While this does fix the exception in case of an empty uri, this doesn’t fix the exception that occurs when the user manipulates the uri (after the pound sign in the browser’s address bar) directly and enters an invalid one. This is where the XAML escape sequence comes into play.


The solution

What I essentially want to do is to change the UriMapper like this:

Read more: infoSupport
QR: http://chart.googleapis.com/chart?chs=80x80&cht=qr&choe=UTF-8&chl=http://blogs.infosupport.com/silverlight-navigation-framework-tip-using-the-xaml-escape-sequence/

Posted via email from Jasper-Net