UriMapping for Silverlight is pretty straight forward and if you use the Silverlight Navigation template you get it all from the template. WP7 contains many of the same features – it’s just not that obvious.
By default a WP7 app navigate using a PhoneApplicationFrame. PhoneApplicationFrame inherits from Frame – the control used in Silverlight. The PhoneApplicationFrame control is exposed as a property named RootFrame in App.xaml.cs.
To enable UriMapping in WP7 from the code you need to follow a few simple steps:
- Create a new method (in App.xaml.cs) that will set the UriMappings you need. In the method you need to make a new instance of UriMapper and for each mapping you wish to create you need to instantiate a new UriMapping specifying the Uri and MappedUri.
- Bind the mappings with the UriMapper instance and set RootFrame.UriMapper to the UriMapper instance.
- Call the method right after the RootFrame have been instantiated – typically in InitializePhoneApplication.
- Modify the existing start page in WMAppManifest.xml to make it fit your mappings.
In the sample below I have added two mappings; a default mapping and a mapping for a detailsview.
To use your mappings simply call your pages using a relative Uri:
You can download a demo here.