Silverlight: Data Retrieval

Overview

There are two ways to retrieve data in Silverlight 1.0. You can either use the standard JavaScript methods, such as XMLHttpRequest, or use the Silverlight Downloader object to fetch image, audio, video, font, along with JavaScript and XAML assets. The Downloader class is restricted to accessing data in the same domain.

This document will concentrate on using the Downloader object. See Using YUI and Silverlight 1.0 for an example of fetching XML data.

Downloader Object

The Downloader object has been modeled after XMLHttpRequest and is very similar in use and limitations. The main difference is the ability to load other types of content and not just XML. It can also download ZIP packages and provides access to the archived files. Downloader provides progress information which is useful when downloading large files.

The main uses are:

  • Download a package containing multiple files
  • Access individual files in packages
  • Retrieve progress information on active downloads
  • Fetch media for MediaElement
  • Fetch fonts for TextBlock

Using the Downloader object with packages of assets makes sense when you have multiple items that are required for your application to run. Combining them into a single download will enhance the performance by eliminating unnecessary network calls. You can use the progress information to provide users with feedback during the download and improve the perceived performance as well.

The examples on this page assume that references to various objects have been saved as properties on the current class.

Download Packages

The Downloader class has three events that will give you progress information about the download and let you provide status updates to the user and also an event when the downloaded content is ready to be accessed.

Downloading individual files and ZIP packages happens in a similar manner. The following example shows downloading a ZIP file, but downloading individual files is accomplished simply by substituting the filename.

Images and Multimedia

The Image, ImageBrush and MediaElement objects allow you to set the source either directly from a URL using the Source property or from assets retrieved using the Downloader object using the SetSource() method.

XAML

XAML documents and fragments can also be downloaded from packages. XAML tends to be verbose and can often quickly grow very long especially with graphics and animations. Luckily XAML is plain text and compresses well and thus makes an ideal item to look at when optimizing Silverlight applications.

Sample

View Sample

Further Reading

Related information on the web is listed below.