Open Links inside the WebBrowser control or outside using IE

August 14, 2011

3 comments

Windows Phone WebBrowser control is a great option for those applications that should host HTML content.

Using the WebBrowser control is so simple, you just have to instantiate one and start using it by just calling the Navigate method:

Code Snippet
  1.             <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
  2.     <phone:WebBrowser Name="webBrowser" />
  3. </Grid>

 

Code Snippet
  1. webBrowser.Navigate(new Uri("blogs.microsoft.co.il/blogs/tomershamam"));

Clicking on links inside the WebBrowser control, navigate to pages inside the WebBrowser control, without closing the application and opening IE.

Sometimes it is preferable opening some links using IE itself, and the question is how?

In Windows Phone there is a special Launcher, for doing exactly that, and it called WebBrowserTask.

So you can create one, providing valid URL and then launch it.

Now the question is: How can I open some links inside the WebBrowser control and others inside IE?

In such case all you have to do is register the WebBrowser.Navigating event, which raises any time you browse to a URL or just clicking a Link. There you can check the arguments for the relevant URL and decide if you want to use the Launcher and cancel the Navigation, or continue on with the WebBrowser control.

Here is a code snippet of how to do that:

Code Snippet
  1. public partial class MainPage : PhoneApplicationPage
  2. {
  3.     // Constructor
  4.     public MainPage()
  5.     {
  6.         InitializeComponent();
  7.  
  8.         webBrowser.Navigate(new Uri("http://blogs.microsoft.co.il/members/Tomer-Shamam.aspx"));
  9.         webBrowser.Navigating += new EventHandler<NavigatingEventArgs>(webBrowser_Navigating);
  10.     }
  11.  
  12.     void webBrowser_Navigating(object sender, NavigatingEventArgs e)
  13.     {
  14.         // In case that e.Uri relevant to be opened in external browser, do as follows:
  15.         if (e.Uri.AbsoluteUri.Contains("blogs.microsoft.co.il/blogs/tomershamam"))
  16.         {
  17.             var task = new WebBrowserTask();
  18.             task.Uri = e.Uri;
  19.             task.Show();
  20.             e.Cancel = true;
  21.         }
  22.     }
  23. }

In the code snippet above, the WebBrowser control is initialized with an address to my profile details. Now clicking on any link that doesn’t include “blogs.microsoft.co.il/blogs/tomershamam” opened inside the WebBrowser control. Otherwise the IE is launched, displaying the relevant link.

imagevimageimageimage

Note: you should save application’s state before launching the WebBrowserTask in case that your application is Tombstoned, specifically in Pre-Mango edition.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

3 comments

  1. AaronJune 28, 2012 ב 22:03

    Thanks very much. Exactly what I needed.

    Reply
  2. Si WEiJuly 12, 2012 ב 17:30

    This approaching is awesome! So there would be no necessary to inject JavaScript to add click listeners to every “a” tag! Thank you!!!

    Reply
  3. SureshFebruary 7, 2013 ב 10:14

    Wow man, you are awesome. I was fighting to resolve this for last one week. There you are. Thank you.

    Reply