Chapter 13. Using External Network Resources

One of the advantages of connected systems is that the network can be used to send or retrieve additional information. Silverlight provides you with several ways to access remote data, either using standard HTTP requests or more complex techniques such as Windows Communication Foundation (WCF).

Using HTTP Resources

The most straightforward way to access external data from Silverlight applications is to use an HTTP request. One convenient class to achieve that is WebClient. It provides you with an asynchronous means of retrieving data. Using an asynchronous approach is better than a synchronous one, since one of the key issues of many web applications is latency. If the server takes a long time to respond, the application stalls until the data is there. When using an asynchronous call, the application continues to run; once the server returns data, the application code can intercept it and use the retrieved information.

The WebClient class supports two important events that you can hook your code into:

DownloadProgressChanged

Whenever the progress of a web request changes, i.e., more bytes have been sent from the server

DownloadStringCompleted

Whenever the web request has been completed

We will implement a simple progress counter using the WebClient class. Example 13-1 contains the UI: a button to start the download, and a text block to output the progress.

Example 13-1. Using the WebClient class, the XAML file (Page.xaml, project DownloadProgress) ...

Get Essential Silverlight 2 Up-to-Date now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.