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

  • New 'Visual Studio Hub' 1-Stop-Shop for GitHub Copilot Resources, More

    Unsurprisingly, GitHub Copilot resources are front-and-center in Microsoft's new Visual Studio Hub, a one-stop-shop for all things concerning your favorite IDE.

  • Mastering Blazor Authentication and Authorization

    At the Visual Studio Live! @ Microsoft HQ developer conference set for August, Rockford Lhotka will explain the ins and outs of authentication across Blazor Server, WebAssembly, and .NET MAUI Hybrid apps, and show how to use identity and claims to customize application behavior through fine-grained authorization.

  • Linear Support Vector Regression from Scratch Using C# with Evolutionary Training

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the linear support vector regression (linear SVR) technique, where the goal is to predict a single numeric value. A linear SVR model uses an unusual error/loss function and cannot be trained using standard simple techniques, and so evolutionary optimization training is used.

  • Low-Code Report Says AI Will Enhance, Not Replace DIY Dev Tools

    Along with replacing software developers and possibly killing humanity, advanced AI is seen by many as a death knell for the do-it-yourself, low-code/no-code tooling industry, but a new report belies that notion.

  • Vibe Coding with Latest Visual Studio Preview

    Microsoft's latest Visual Studio preview facilitates "vibe coding," where developers mainly use GitHub Copilot AI to do all the programming in accordance with spoken or typed instructions.

Subscribe on YouTube