Mapping as we described it earlier basically involves earmarking a resource as being represented by a Url e.g. MainView.xaml represents ‘Pages/MainView/” Url. One notable thing here is that our approach to mapping is explicit, as opposed to being implicit – think about how on Web Servers, file paths implicitly get mapped to Url paths. Explicit mapping, just like in ASP.NET MVC, provides a number of benefits like greater flexibility, use of Url tokens, cleaner Urls, allows for non-navigation uses, and also help us gets away from the temporal nature of files/namespaces etc. Though that is not to say, you couldn’t switch to some sort of implicit mapping with nRoute, just that it’s not provided out of the box.

Mapping Navigation Content

In line with our attribute based approach to resourcing, mapping to a visual controls can be done using a MapNavigationContent attribute. The MapNavigationContent attribute just takes in a relative Url string parameter, and can be tacked onto a navigation content type as such:

  1: [MapNavigationContent("Pages/HomePage/")]
  2: public partial class Home : UserControl
  3: {
  4:     ...
  5: }

Further, you can even have more than one Url associated with a given type, this allows for some advanced scenarios wherein you can change the behaviour or visuals of a type per the requested Url. Also, another equivalent way to map a content type with a Url is to use the DefineNavigationContent attribute, this as described earlier, doesn’t apply the mapping onto the decorated type, rather it just defines a mapping without context to the applied on type; consider:

  1: [assembly: DefineNavigationContent("Pages/HomePage/", typeof(Home))]

Above, we are saying associated the given Url with the “Home” type – and this has no association to the assembly type, other than to piggy-back onto it. Define based mappings are often useful to either keep all the mappings in one place or when you don’t have ownership of the targeted type.

Mapping Navigation Resources

If you’ve noticed above, with MapNavigationContent attributes we were actually tying clr-types to the Urls – but that wouldn’t work with resources that don’t have types, eg. loose xaml files, images, embedded resources etc. For that we use a DefineNavigationResource attribute, which can handle some known resource types such as text, images, loose xaml, x-class xaml, and binary data. To use it is quite similar to DefineNavigationContent, consider:

  1: [assembly: DefineNavigationResource("Pages/LogoPicture/", "Images/Logo.png", KnownResourceType.BitmapImage)]

Above we are defining the “Pages/LogoPicture/” Url to return an embedded Logo.png resource. Furthermore, to support custom use-cases one can override the MapNavigationBase attribute or use the static mapping technique described below.

Static Mapping

Given under the hood we have ASP.NET like routing engine, we can also register routes directly against the NavigationService (or RoutingService), consider:

  1: NavigationService.MapRoute(new Route("Pages/HomePage/", new NavigationContentHandler((p) => new Home())));

Above we are just saying for the Url “Pages/HomePage/”, let it be handled by the NavgationContentHandler, which in-turn returns a new instance of “Home” type. Further, just like in ASP.NET/MVC along with the route you can define constraints, default parameters, or even custom IRouteHandler implementations, consider:

  1: NavigationService.MapRoute(new Route(
  2:     "Pages/Customer/{Mode}/{Id}", 
  3:     new ParametersCollection(new { Mode = 'View', Id = (string)null }), 
  4:     new DynamicContentHandler(typeof(CustomerPage))));

Also it shouldn’t come as a surprise that the built-in MapNavigationXXX attributes based approaches ultimately boil-down to being routes registered against the routing engine.

Last edited Aug 29, 2010 at 11:07 PM by Orktane, version 4

Comments

No comments yet.