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

  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

Subscribe on YouTube