Launching the Browser
       by kirupa  |  3 August 2010

In Windows Phone 7, you can navigate to web sites using the browser just as easily as you can navigate between pages of your application. Here is a really short video that shows how clicking a button launches the browser with a URL the application the specified:

[ a small video showing what you will eventually create ]

In this short tutorial, you will learn how to allow your applications to call the browser to load a URL that you specify.

Getting Started
Make sure you have everything up and running to do Windows Phone development, and go ahead and create a new Windows Phone project in Expression Blend.

Once your new project has been created, insert a Button control and assign its click event to an event handler. You can do that easily by selecting the Button, clicking on the Events tab in the Properties Inspector, and giving an event handler name next to the Click event:

[ giving the Click event an event handler whose name is LaunchURL ]

Once you have done this, you'll be taken to the Code view where you can type some code. This is where things get fun.

Say Hello to WebBrowserTask
The class responsible for helping you launch the browser and pass a URL to display is WebBrowserTask. The example code for using this is:

WebBrowserTask browserTask = new WebBrowserTask();
browserTask.URL = "";

Unfortunately, WebBrowserTask doesn't live in any of the default namespaces that your code file would have. This means that you will need to add a using statement to the top of your file. The statement you need to add is:

using Microsoft.Phone.Tasks;

If you are using Visual Studio, which you should since it is better suited for code-editing than Expression Blend, you can just click on the small icon that appears when you hover over WebBrowserTask and automatically have the correct using statement added:

[ Visual Studio is great for writing code thanks to niceities like this ]

In either case, regardless of whether you had to manually type the using statement in Expression Blend or point and click using Visual Studio, just make sure that Microsoft.Phone.Tasks is there at the top of your code file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

Getting to what we started with, if you created an event handler to go with the Click event on your Button, paste the three lines of code you saw earlier into the event handler itself:

private void LaunchURL(object sender, RoutedEventArgs e)
WebBrowserTask browserTask = new WebBrowserTask();
browserTask.URL = "";

If you click/tap on your button, the URL you specified would load in the browser!

Looking at the Code
The code for making this work is pretty straightforward, so let's go ahead and look at the three lines in slightly more detail...starting with the first line:

WebBrowserTask browserTask = new WebBrowserTask();

In this line, I am declaring and creating my WebBrowserTask object, and I am calling it browserTask. By doing this, all of the powers bestowed upon the WebBrowserTask class have been transferred to the browserTask object.

In the next line, I specify the URL to load when the browser is launched:

browserTask.URL = "";

The URL property takes a string as its value. In my case, I have the string literally hardcoded to This line is optional by the way. You can get away with not setting the URL property because, in that case the browser will display its default home page.

Ok, we are at the last line:


Saying that this line is the most important one for displaying the browser would be a major understatement. The Show() method is responsible for actually taking what you've done in the previous lines and calling the browser with your property values passed in. Without this line, no browser will appear regardless of how awesome your URL property may have been!

Well, that's all there is to it. As you can see, the code for making this all work is pretty straightforward and consists of only three lines of code if you want to launch the browser with a URL you already had in mind.

If you want to take apart my example, feel free to download the source file below:

Download Source Files

Got a question or just want to chat? Comment below or drop by our forums (they are actually the same thing!) where a bunch of the friendliest people you'll ever run into will be happy to help you out!

When Kirupa isn’t busy writing about himself in 3rd person, he is practicing social distancing…even on his Twitter, Facebook, and LinkedIn profiles.

Hit Subscribe to get cool tips, tricks, selfies, and more personally hand-delivered to your inbox.


SUPPORTERS:'s fast and reliable hosting provided by Media Temple.