WP8 Uri association with Caliburn.Micro

Windows Phone 8 has this new feature of opening an app (in this case my own app) by assigning Uri. It is useful when you are doing Oauth and need to supply the redirection uri. If you are using this new feature, then your app can be automatically opened after oauth is done.

You can follow this tutorial to get up to speed with Uri Association and Oauth with Windows Phone. It should be pretty easy to implement this.

But chances are, you might be using some sort of MVVM framework to structure your app. If you don't, I encourage you to take a look at Caliburn.Micro.

After bashing my head few times, I finally managed to get it to work with Caliburn. The trickiest part is to understand how properly override the RootFrame.

In your bootstrapper.cs

    protected override PhoneApplicationFrame CreatePhoneApplicationFrame()
        // var frame = base.CreatePhoneApplicationFrame(); this doesnt work
        var frame = new PhoneApplicationFrame(); // this works
        frame.UriMapper = new AssociationUriMapper();

        return frame;

And `AssociationUriMapper.cs` (as per the example in the link above)

    public class AssociationUriMapper : UriMapperBase
        private string tempUri;

        public override Uri MapUri(Uri uri)
            tempUri = System.Net.HttpUtility.UrlDecode(uri.ToString());

            // URI association launch for contoso.
            if (tempUri.Contains("pocketthis:MainPage"))
                // Get the category ID (after "CategoryID=").
                //int categoryIdIndex = tempUri.IndexOf("CategoryID=") + 11;
                //string categoryId = tempUri.Substring(categoryIdIndex);

                // Views/MainPage.xaml returns external exception, 
                // so remember the / before views
                return new Uri("/Views/MainPage.xaml", UriKind.Relative);

            // Otherwise perform normal launch.
            return uri;