Geeks With Blogs
Mister Goodcat Purrfect software since 1882

Today I was working with a small Silverlight application that is using the new navigation feature. Basically I adapted what Tim Heuer (thank you for your great videos and posts!) shows in his tutorial here. Apparently that sample has not been updated for the final version of Silverlight 3. When you watch that video and download the sample code, you'll notice that although it will compile under Silverlight 3 RTM, running it will result in a white screen. In Firefox that is -- curiously enough IE partly shows the sample, but it's not working. Unfortunately, you don't receive any runtime exceptions, even with the debugger attached. In the error console however, the following Java Script exception is logged:


Error: Unhandled Error in Silverlight Application
Code: 4004 
Category: ManagedRuntimeError     
Message: System.ArgumentException: Navigation is only supported to relative URIs that are fragments, or begin with '/', or which contain ';component/'.
Parameter name: uri
[...]


So the first thing is to change all the URIs used in the sample and add a preceding slash to them. In particular, you need to change the Tag properties of the HyperlinkButtons in MainPage.xaml, the parameter of the call to Navigations.Navigate(...) in CustomerList.xaml.cs, and of course the mappings defined in the resources of App.xaml (hint: you could also use the NavigateUri property of the hyperlink buttons instead of Tim's approach, which makes the sample even easier).

<navcore:UriMapper x:Key="uriMapper">
    <navcore:UriMapping Uri="/Home" MappedUri="/Views/HomePage.xaml" />
    <navcore:UriMapping Uri="/About-Us" MappedUri="/Views/AboutPage.xaml" />
    <navcore:UriMapping Uri="/Customers" MappedUri="/Views/CustomerList.xaml" />
    <navcore:UriMapping Uri="/Customer/{id}"
        MappedUri
="/Views/CustomerDetail.xaml?id={id}" />
</
navcore:UriMapper>

(Precede all occurrences of the URIs with a slash, exemplarily shown for the UriMapper here)

After that, you'll receive a different Java Script exception:

Error: Unhandled Error in Silverlight Application No XAML was found at the location '/Home'.
[...]

The reason for that is a bit more tricky to find out. In the beta version of Silverlight 3, it was sufficient to provide an UriMapper with a certain key (e.g. in the App's resources), and that magically was used throughout the application. Silverlight 3 RTM requires the UriMapper property of a navigation frame to be set explicitly. You can bind that property (in MainPage.xaml) to the static resource defined in App.xaml:

<navigation:Frame x:Name="MainFrame" UriMapper="{StaticResource uriMapper}"

Now the application basically is working: you can navigate through the pages, and all content is displayed correctly. You can also use the corresponding methods to go back and forward programmatically. However, you'll notice that the navigation buttons of the browser won't work. The address bar won't be updated also, and neither will deep linking work. Why is that? Well, it took me four hours (shame on me?) to find out why, and that evil little detail is the reason I've decided to write this post after all.

I started reading through the breaking changes document, but I couldn't find anything helpful regarding my problem. I played around with some of the Frame's properties, like "JournalOwnership", which sounded promising but didn't change anything either. Of course I tried searching the web, but apparently few or no people ran into this issue. Then I tried a different approach and built a new Silverlight navigation application (Visual Studio has this nice template...) from scratch. Of course it worked like a charm. And I couldn't see anything that was done differently from my own application. But this template already is pretty sophisticated, using styling and all, so I began to take out all the unneeded stuff until nothing more than maybe 20 lines of code were left. Basically two hyperlink buttons, the frame, and two pages, consisting of nothing more than text blocks. And still it worked, whereas my own, equally pared-down application did not. In desperation, I started hosting the application in IIS (no difference), and then finally started the Silverlight project directly (instead of using the development server) - and BAM! It worked.

This gave me the final push I needed. There must be something wrong with the generated page, because when you start the Silverlight application directly, it dynamically generates a temporary HTML page, which obviously was different in some detail from the HTML page of the web project in the sample code. And then I saw it: the IFrame used for the history had an id attribute set, whereas the HTML page from the sample had not. I added

<iframe id="_sl_historyFrame" ...

to the IFrame tag and the sample worked instantly...

This is one of the bad examples I'll remember for the next years, where a small hint in a release notes document or similar would have saved me hours of frustration. I also hope Tim will update his tutorial soon because probably others will run into this issue too, especially because his sample is not marked as outdated or for SL3 beta only.


Posted on Tuesday, July 14, 2009 11:08 PM Silverlight | Back to top


Comments on this post: Breaking changes in Silverlight's navigation framework

# re: Breaking changes in Silverlight's navigation framework
Requesting Gravatar...
Thanks for this...it was one I hadn't marked for updating but need to!
Left by timheuer on Jul 15, 2009 8:38 PM

# re: Breaking changes in Silverlight's navigation framework
Requesting Gravatar...
You are the best bloger in the whole world!

I have been searching this phrase "no xaml was found at the location" for days now. Only 1 web result and now yours is the 2nd. Great job, but I still have problems with the navigation framework with an upgraded sl2 app...

Lucky for me the 1st thing I did was to check the "Auto generated" document of the new SL3 apps. It really does have what you report. For other researchers out there, in Tim Heuer's blog also says that UserControls are not supported in the navigation framework (although they were on the beta 3).

I am still unlucky and still (!) haven't found a solution to my problem. It just refuses to work giving always the above error.

Anyway, GREAT POST!
Left by everydaypanos on Jul 15, 2009 10:26 PM

# re: Breaking changes in Silverlight's navigation framework
Requesting Gravatar...
Hi Panos! First of all thank you! Regarding your problem: if you have a simple sample application that demonstrates your errors, feel free to send it to me.
Left by Mister Goodcat on Jul 15, 2009 11:36 PM

# re: Breaking changes in Silverlight's navigation framework
Requesting Gravatar...
OMG, You rock, dude.

I was beating my head against the wall on this one. I even looked back in history and didn't see the iframe. I was using the Silverlight server control (that they did away with), so I assume it was adding it.

This is the most useful thing I read all day. Thanks.
Left by Brian Genisio on Aug 13, 2009 10:28 PM

# re: Breaking changes in Silverlight's navigation framework
Requesting Gravatar...
Thank you, i was also struggling to get it to work.
Notice I had to take out "/" from the navcore uri parameter:

<navcore:UriMapping Uri="Home" MappedUri="/Views/HomePage.xaml" />

Working on Visual Studio 2010 Beta1

Thank you again.
Left by Henry on Aug 25, 2009 9:22 PM

# re: Breaking changes in Silverlight's navigation framework
Requesting Gravatar...
Can some kind sole upload a working SL3 version of this for people who are learning this new technology?
Left by Hunt on Oct 23, 2009 4:21 PM

# re: Breaking changes in Silverlight's navigation framework
Requesting Gravatar...
I came here via the comments to tim orignal video. For people still struggling, add

UriMapper="{StaticResource uriMapper}"

To your frame.

See http://forums.silverlight.net/forums/p/108580/246863.aspx

Worked for me :)
Left by Jammin on Dec 15, 2009 7:30 PM

# re: Breaking changes in Silverlight's navigation framework
Requesting Gravatar...
Thanks a lot
it saved me from breaking my head...
Left by Rama charan on May 03, 2010 6:49 AM

# re: Breaking changes in Silverlight's navigation framework
Requesting Gravatar...
Wow finally found this page after searching pretty hard, the way this was being created in SharePoint was missing the iframe. Doh!
Left by Jc on Nov 04, 2010 12:47 AM

Your comment:
 (will show your gravatar)


Copyright © MisterGoodcat | Powered by: GeeksWithBlogs.net