Implementing WebClient Class to Download Resource From Web Services in Windows Phone Application


For our application to be more widespread in today's world, it needs to connect with the Outside World. As in the current market scenario Web Based applications are in high demand compared to standalone apps.

So in this article we will implement a way to connect to the Web Resources viz the WebClient Class.

We will implement the WebClient class.


Step 1: Create a new “Windows Phone Application” in Visual Studio and name the project as you choose (I here named it WebService).

Now a new Windows Phone Application Page (MainPage.xaml) will be generated.

Step 2: Now go to the toolbox and add a Button Control, a TextBox Control and a WebBrowser control to your project and set the visibility property of the WebBrowser control to collapsed.

Your MainPage.xaml will look like this:

<!--ContentPanel - place additional content here-->

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

<Button Content="Download Me!!!" Height="196" HorizontalAlignment="Left" Margin="75,190,0,0" Name="buttonDwnld" VerticalAlignment="Top" Width="314" Click="buttonDwnld_Click" />

<TextBlock Height="76" HorizontalAlignment="Left" Margin="45,42,0,0" Name="textBlockDwnldStat" Text="" VerticalAlignment="Top" Width="378" FontSize="25" />


And your MainPage.xaml will look like this:

web Services

Step 3: Navigate to the MainPage.xaml.cs file of the project and add the following code initially:

public partial class MainPage : PhoneApplicationPage


    //Creating an Instance of WebClient Class

    WebClient webclient = new WebClient();

Step 4: Now in the Constructor part add the folllowing Code:

// Constructor

public MainPage()



        //Generating two Events:

        //1-DownloadProgressChanged-- when the content

        //is downloaded from the web

        //this event is continuously fired and it's a

        //good place to show your download progress

        webclient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webclient_DownloadProgressChanged);

        //2-DownloadStringCompleted-- It is Fired when

        //the Content is Fully Downloaded from the Web

        webclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webclient_DownloadStringCompleted);


Step 5: Now in the Button Event handler add the following code:

private void buttonDwnld_Click(object sender, RoutedEventArgs e)


    //Asynchronously Download the Content from the

    // websource passing as Uri

    webclient.DownloadStringAsync(new Uri(""),"csharp");


Step 6: Now go to DownlaodProgressChanged Method of DownloadProgessChanged Event and add the following Code:

void webclient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)


        //Checking If the UserState viz is the Propert

        //of DownloadProgressChangedEventArgs

        //gets the source of download

        if (e.UserState as string == "csharp")


            //Showing the Total number of bytes

            //Downloaded by using the BytesReceived

            //Property in the textBlock                  

            textBlockDwnldStat.Text = "Total bytes

            Received: " + e.BytesReceived.ToString();



Step 7: Similarly go to the DownloadStringCompleted method and add the following code:

void webclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)


    //Checking if no error is there and the

    //Downloading is not cancelled

    if (e.Error == null && !e.Cancelled)


        //Showing the Result viz is actually a xml

        // in the MessageBox




That's all for this article, compile your project and run it. You will see as you press the "Download Me!!!" button in your application you will get the total bytes received from the web source in the TextBlock and the XML result is shown in the MessageBox.

I am embedding the source file so that you can go through it.

Similar Articles