Silverlight Browser…Mapping
(Note: if you’ve just stumbled upon this post, please be sure to read the first three posts in the series here, here, and here to get an understanding of what we’re trying to achieve and where we’re at.)
In my last post I built out the shell we are going to need for our browser. After I did so I realized there was a key mistake. In that version of the code I had placed the menu and application path components within the navigation toolbar. The problem with this approach is that if I choose to configure the navigation toolbar either to always be hidden or only display when running out of the browser, I’ll lose the functionality of these components when running in the browser as they will be hidden. To correct this issue I moved the menu and application path components to the browser itself. I also added display properties to the browser to allow a more granular control of when these components should be displayed. Now that we’ve gotten this issue squared away let’s start moving forward.
Our next task is to built out the menu and application path components our browser will consume. My initial thought was to follow the pattern ASP.NET uses with it sitemap, sitemappath, and menu components. I went so far as to download the .NET Framework source code from here to take a look at how Microsoft does it. While I was fairly impressed with what I saw, I felt that it was just a bit too much for the task at hand, so I decided to only use the pieces that I thought were appropriate for our task.
After I had a general design direction I decided to get to work. The first think I did was to add a new project entitled Silverlight.Navigation.Mapping to our solution. Here’s what the current version of the solutions looks like:

In the Silverlight.Navigation.Mapping project it all starts with the MenuItem class. This is an abstract class that only contains a three properties the following three properties:
Description- a long description of the menu item (for tool-tips).
ImageUri - the uri of an image to use to represent the menu item (optional).
Title - the title of the menu item to use in visual display.
There are three classes that then inherit from the MenuItem class:
HomeMenuItem - this represents the “home page” of the application and simply contains a ContentUri property that points to the page.
MainMenuItem - this represents a top level menu item of the application. This class has one property SubMenus, which is a collection of SubMenuItems. A MainMenuItem itself doesn’t point to a page, it just serves as a way to logically group pages together.
SubMenuItem - this represents a page within the application. The ContentUri property points to the page, and the Parentproperty serves as a reference to the MainMenuItem containing the submenu.
You’ll note here that I’ve essentially limited the depth of the application to two nodes (Main Menu and Submenu). This was an intentional decision. Often times I come across apps with really deep menuing structures where it’s very easy to get lost and I’d like to avoid that, at least for the time being. I probably shouldn’t force this decision and allow implementers to make an app as confusing as they want, but this can be addressed at a later date.
Next up is the ApplicationMapclass. This is an abstract class that represents the map of an application. The properties and methods of the class are pretty self-explanatory. The key properties are the abstract HomeMenuItem and MainMenuItems properties. Inheritors of this class are responsibly for defining the implementation for this properties.
Lastly we have the XmlApplicationMapclass. This class inherits the ApplicationMap class and implements the HomeMenuItem and MainMenuItems properties by loading their values from an xml document. Pretty simple, and fairly consistent with the ASP.NET model
The UI implementation is pretty simple at this point. As you’ll see in the picture below, I’ve added several new controls to the project.

ApplicationMapPath- similar to ASP.NET’s SiteMapPath, this just keeps track of where the user is in the application.
MainMenu - this simply display the main menu items defined for the application.
SubMenu- this is used to display the submenu options for a given main menu.
The Browser control has an ApplicationMapPath control and a MainMenu control. When the user clicks a main menu item the SubMenu control is displayed for the main menu. When a submenu item is clicked the app navigates to the page defined by the submenu item and the ApplicationMapPath control is updated to show the path to the current page.
The actual implementation is a snap. If you look at the demo project you’ll see I created an ApplicationMap.xml file that contains the definition of our application. I also created some pages under the Views folder. These views aren’t much of anything to look at right now, but they help to illustrate the point.
If you look in Application_Startup method of App.xaml.cs you’ll see the following code:

Here’s what’s going on:
1. I create a new instance of the XmlApplicationMap class using the ApplicationMap.xml file I created in the project.
2. I create a new instance of the Browser control from Silverlight.Navigation.Controls, passing in the XmlApplicationMap class as the constructor’s parameter.
3. I set couple of UI preferences (DisplayNavigationToolbar and IsNavigationToolbarExpanded) on the browser.
4. Finally, I set the root visual of the app to the instance of the browser, and voila, my menus and application map are up and running. Both in and out of the browser!
In Browser:

Out of Browser:

I’ve issued a new release on the CodePlex site for this project that contains the functionality discussed above. You can download the release here.
I’ve also put this app up on my website here. Per Tim Heuer’s post on Silverlight 3 beta install guidance, I customized the install experience to let folks know if they need Silverlight 3 and what it means to install the beta version of Silverlight 3 at this point in time.






