Mobile Corner

UriMapper in Windows Phone

Nick Randolph looks at how you can use a UriMapper to help structure your Windows Phone application.

In an earlier post on Search Extensibility in Windows Phone 7, a UriMapper was used to translate the launch URI invoked by Bing Search into a MappedUri; in this case, a page within the Windows Phone application. In this article we're going to look at how to use a UriMapper to aid in structuring and navigating within your Windows Phone application. As a refresher, here's the UriMapper we defined in the App.xaml, and how it was connected to the PhoneApplicationFrame in the App.xaml.cs file.

App.xaml
<Application.Resources>
    <uriMapper:UriMapper x:Key="UriMapper">
            <uriMapper:UriMapping Uri="/SearchExtras"
                            MappedUri="/MainPage.xaml" />
    </uriMapper:UriMapper>
</Application.Resources>

App.xaml.cs
RootFrame = new PhoneApplicationFrame() {
                                UriMapper = Resources["UriMapper"] as UriMapper
                            };

We're going to start by changing the UriMapping to the following:

<uriMapper:UriMapping Uri="/{Page}" MappedUri="/{Page}Page.xaml" />

This mapping will translate any URI that starts with "/" by appending the "Page.xaml" suffix. This assumes that all the pages within our application will end with "Page.xaml", but means that instead of navigating to "/SecondPage.xaml", we can instead navigate to just "/Second".

This becomes useful if we later decide that all our pages are going to be in a folder called "Pages". Previously, you would have had to search through your entire application looking for any Navigate methods and modify the URI to include the "/Pages" prefix. Using UriMapping, all you need to do is change the mapping to include the prefix.

<uriMapper:UriMapping Uri="/{Page}" MappedUri="/Pages/{Page}Page.xaml" />

As your Windows Phone application grows, you may decide to break your application up into separate assemblies. In this case, you can again use a UriMapping to locate the page to which the user is navigating. For example, the following mapping will locate the MySatellitePage.xaml, which is in an assembly called Satellite:

<uriMapper:UriMapping Uri="/MySatellite" 
                      MappedUri="/Satellite;component/Pages/MySatellitePage.xaml" />

Deep Linking
Another place where the use of a UriMapping is important is when you're using deep linking in order to direct users to a specific location within the application. This may be from a Live Tile (as determined by the URI specified when creating the tile), or from a Toast notification (the Param element determines the URI within the application to be navigated to when the user taps on the toast).

The simple approach is to use the URI of the page you want to launch (e.g., "/MyLaunchPage.xaml"). However, this introduces a strong coupling between the application structure and the notifications being sent from the server. Instead, we can add a UriMapping similar to the following, which maps a "/Toast" URI through to the launch page:

<uriMapper:UriMapping Uri="/Toast" 
                      MappedUri="/MyLaunchPage.xaml" />

Now, if we need to change the structure of the application, we can do so easily by updating the mapping. Without the UriMapping we'd have to update both the Windows Phone application and the server code, and ensure they're both updated at the same time – a nearly impossible task.

You'll notice that we haven't specified any query parameters in any declared mappings. This is because they'll automatically get mapped across from the URI to the MappedUri. In the case of the last UriMapping, for example, a URI of "/Toast?customerId=1234" will get mapped to "/MyLaunchPage.xaml?customerId=1234".

I've demonstrated a number of scenarios where a UriMapper can be used to improve navigation and the structure of your Windows Phone application. Learn to use this powerful tool, and you'll be writing more efficient code before you know it.

About the Author

Nick Randolph runs Built to Roam, a consulting company that specializes in training, mentoring and assisting other companies build mobile applications. With a heritage in rich client applications for both the desktop and a variety of mobile platforms, Nick currently presents, writes and educates on the Windows Phone platform.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube