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.
// Get a reference to the Silverlight plug-in
var plugin = sender.GetHost();
// Create a new Downloader object to use
var downloader = plugin.CreateObject("Downloader");
// Hook up events
downloader.AddEventListener("DownloadProgressChanged",
Silverlight.CreateDelegate(this, this.downloader_DownloadProgressChanged));
downloader.AddEventListener("Completed",
Silverlight.CreateDelegate(this, this.downloader_Completed));
downloader.AddEventListener("DownloadFailed",
Silverlight.CreateDelegate(this, this.downloader_DownloadFailed));
// Start download of "Assets.zip"
downloader.Open("GET", "Assets.zip");
downloader.Send();
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.
// Set image source directly on an Image object
this.imgSample1.Source = "preview.png";
// Set a downloaded image as the source for an Image object
this.imgSample1.SetSource(sender, "");
// Set image from a downloaded package, with the filename "preview.png", to an Image object
this.imgSample1.SetSource(sender, "preview.png");
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.
// Get a reference to the Silverlight plug-in
var plugin = sender.GetHost();
// Get a fragment of XAML and add it to the root of the application
var xamlObject = plugin.Content.CreateFromXamlDownloader(sender, "NewButton.xaml");
// Add to the display
this.myRoot.Children.Add(xamlObject);
Sample
function MyApplication9() { }
MyApplication9.prototype = {
Initialize: function(plugIn, userContext, rootElement)
{
// Get and store object references
this.txtAction = rootElement.FindName("txtAction");
this.txtStatus = rootElement.FindName("txtStatus");
// Create a new Downloader object
this.downloader1 = plugIn.CreateObject("Downloader");
// Hook up events
this.downloader1.AddEventListener("Completed",
Silverlight.CreateDelegate(this, this.downloader1_Completed));
this.downloader1.AddEventListener("DownloadProgressChanged",
Silverlight.CreateDelegate(this, this.downloader1_DownloadProgressChanged));
// Save the current time
this._startTime = new Date();
// Start downloading
this.downloader1.Open("GET", "MyApplication9/preview.png?" + Math.random());
this.downloader1.Send();
},
downloader1_Completed: function(sender, e)
{
var totalTime = new Date() - this._startTime;
this.txtAction.Text = "Completed in " + totalTime + "ms";
this.txtStatus.Text = "100%";
/* Other things that you could do with downloaded content
// Set a downloaded image as the source for an Image object
this.imgSample1.SetSource(sender, "");
// Set image from a downloaded package, with the filename "preview.png", to an Image object
this.imgSample1.SetSource(sender, "preview.png");
*/
},
downloader1_DownloadProgressChanged: function(sender, e)
{
var percent = Math.floor(sender.DownloadProgress * 100);
this.txtStatus.Text = percent + "%";
}
}
Further Reading
Related information on the web is listed below.

