Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

101 How to on Windows Phone #11 – Web Browser Task in Windows Phone

DZone's Guide to

101 How to on Windows Phone #11 – Web Browser Task in Windows Phone

Free Resource

In this article we are going to see how to use the Web Browser task in Windows Phone application development. With Web  Browser Task we can provide an option for the end user to launch the web browser application and specify a static or a dynamic URL based on the requirement. When using the Web Browser task application memory usage is not counted which will be very useful while using in a low end devices. Let us see the steps on using the Web Browser Task in a windows phone application.

Open Visual Studio 2010 IDE and create a new Silverlight for Windows Phone project with a valid project name as shown in the screen below. Once the project is created add some controls which are used to open the Web Browser Task as shown in the screen below.

image

Now drag and drop few controls to the screen which is used to trigger event that launches the Web Browser task with the url specified by the end user. Once we designed the screen with the controls we can see the screen looks like below.

image

Now we need to write our code in the button click event to trigger the Web Browser task on the user click. To do that just go to the code behind and first add the USING handler code on top with the existing using statements as shown in the code below.

using Microsoft.Phone.Tasks;

Next is to add the below code to the button click event which basically creates an instance of the Web Browser task which opens the specified url in the browser as shown in the code below.

Code:

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;

namespace F5debugHowto11
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            WebBrowserTask wbtask = new WebBrowserTask();
            wbtask.Uri = new Uri("http://f5debug.net", UriKind.Absolute);
            wbtask.Show();
        }
    }
}

image

Now we are done with our code, just run the application by pressing F5 directly from the keyboard or we can use the Build and execute the project option from the tool bar to run the application. Once the Build is successful we can see the Windows Phone emulator with the application and the expected outputs as shown in the screens below.

 

Output Screens:

image

 

So in this article we have seen how to use the Web Browser task that is used to open a specified url in the browser. Web Browser task is supported in both Windows Phone 7 and 7.1 versions. That’s it from this short tutorial on Windows Phone see you all in the next tutorial soon. Mean while Happy Programming!!!

Topics:

Published at DZone with permission of Karthikeyan Anbarasan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}