Wahlin on .NET

Silverlight XAML Primer 9: Using the Silverlight Downloader Object

Minimize the time users wait and provide more efficient user interfaces with this code.

Silverlight can play and display a variety of file types ranging from video to .MP3s to images. Often, files can be downloaded on demand without affecting application performance; sometimes, though, the size of a file may cause considerable delays as the file is downloaded.

Fortunately, Silverlight provides a built-in download mechanism that can be used to download large files or a lot of little files that have been compressed into a .ZIP file. In this article, I'll introduce you to the fundamentals of using Silverlight's file download capabilities and show how they can be used to speed-up application load times while allowing users to view load progress.

Downloading Files
A built-in object called the "Downloader" gives you the ability to download files "on the fly" while a Silverlight application is running. The Downloader object is capable of downloading standalone XAML and media files or files compressed into a .ZIP archive. It makes the downloaded files accessible on the client-side with surprisingly little code.

Several events are exposed by the Downloader -- including Completed, DownloadFailed and DownloadProgressChanged -- that can be used to notify end users about the status of a download as needed. The table describes these events:

Event

Description

Completed

Fires when the downloaded file has completed.

DownloadProgressChanged

Fires incrementally as a file is being downloaded.  This event can be used to provide a user with a progress indicator.

DownloadFailed

Fires when the download of a file fails.

You create an instance of the Downloader object invoking the Silverlight control's createObject() method at runtime using JavaScript and then attaching its events to event handlers. Listing 1 shows an example of creating a Downloader object and attaching it to event handlers to handle the Completed and DownloadProgressChanged events. The Downloader is used to retrieve a .ZIP file that contains additional XAML content used by the application.

The code in Listing 1 first creates an instance of the Silverlight control and hooks the onLoad event to the event handler named onLoad(). Within the onLoad() event handler, the Downloader object is created using the Sivlerlight control's createObject() method. Version 1.0 of Silverlight only accepts a value of "downloader" when calling createObject() but it may accept other values in future versions.

Once the Downloader object is created, the code wires up the object's DownloadProgressChanged and Completed events to event handlers by calling AddEventListener(). Next, the Downloader calls the Downloader object's Open() method to identify the HTTP verb used to get the file as well as the file's location. Finally, the Send() method is called to start the download.

Listing 2 shows the event handlers for the Completed and DownloadProgressChanged events defined in Listing1. The event handler for the onProgressChanged event handler updates a TextBlock element with the download progress percentage while the onCompleted event handler updates the XAML canvas with additional XAML content that was obtained from the downloaded .ZIP file.

In cases where text content is downloaded directly (XAML files, .JS files, etc.), you can call the sender object's ResponseText property to access the data contained in the downloaded file. The example shown in Listing 1 and Listing 2 downloads a .ZIP file and accesses the compressed XAML file by calling the CreateFromXamlDownloader() method (see Listing 2). The sender object is passed as the first parameter followed by the name of the XAML file as it appears within the .ZIP file (Buttons.xaml, in this example). 

The CreateFromXamlDownloader() method automatically parses the XAML text and creates a XAML object that can then be added into the main application's canvas. Alternatively, you could call the GetResponseText() method passing in the name of the file within the .ZIP archive that you'd like to retrieve. Calling this method returns a string that can then be fed into the CreateFromXaml() method to create a XAML object.

Silverlight's Downloader object can be used in any situation where one or more files need to be downloaded and used in an application. By using it effectively, you can minimize the time users wait and provide more efficient user interfaces.

About the Author

Dan Wahlin (Microsoft MVP for ASP.NET and XML Web Services) is the founder of The Wahlin Group which specializes in .NET and SharePoint onsite, online and video training and consulting solutions. Dan also founded the XML for ASP.NET Developers Web site, which focuses on using ASP.NET, XML, AJAX, Silverlight and Web Services in Microsoft's .NET platform. He's also on the INETA Speaker's Bureau and speaks at conferences and user groups around the world. Dan has written several books on .NET including "Professional Silverlight 2 for ASP.NET Developers," "Professional ASP.NET 3.5 AJAX, ASP.NET 2.0 MVP Hacks and Tips," and "XML for ASP.NET Developers." Read Dan's blog here.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube