NavigationService in WP throws Exception when navigating to Web URL


As a WPDev, you already know about the NavigationService class that we use regularly to navigate from one page to another. But what if you want to navigate to a web URL? If you use the same class for web navigation, you will face a ArgumentException unhandled in your App class.

 

If you are one of the victim and came to this page by searching online for the solution, this post will help you today to resolve the exception in your code.

 

Last day while building a Windows Phone application, one of my friend did a common mistake and used the NavigationService class to navigate to a web URL and when he executed the code, immediately encountered the following exception message directly unhandled to App.xaml.cs file and there was no other way to handle this crash:

 

Navigation is only supported to relative URIs that are fragments, or begin with '/', or which contain ';component/'. Parameter name: uri

 

Here is a screenshot of the exception message that you will encounter when you try to navigate to web URL using the NavigationService class:

 

ArgumentException when calling NavigationService in WP8 to navigate to an URL

 

I thought to blog about this issue as many poor novice will find it useful when face the similar exception unknowingly. The NavigationService.Navigate(…) method can only navigates to a relative path/Uri fragments available within the same package. So, if you want to navigate to an external website URL, you have to use WebBrowserTask instead of NavigationService.

 

To implement the same, first create an instance of the WebBrowserTask and populate the Uri property with the web url where you want to navigate the user. Then call the Show() method of the class to open up the web browser control and navigate the user to the specified page. Here is the code snippet of the same for your reference:

 

// Create the instance of the WebBrowserTask, set the Uri property to the URL where you want to
// navigate and then call the show method of the class instance.
new WebBrowserTask { Uri = new Uri("http://www.kunal-chowdhury.com", UriKind.Absolute) }.Show();

 

I hope, this post will help you to understand how to navigate to an external web page from a Windows Phone application. Feel free to ask any queries that you are facing. Happy coding!


0 comments


 
© 2008-2014 Kunal-Chowdhury.com | Designed by Kunal Chowdhury
Back to top