Chapter 16. Special Silverlight JavaScript APIs

Advanced JavaScript APIs

This chapter is a mix of the Silverlight JavaScript capabilities introduced in the second part of this book and the JavaScript access to Silverlight content presented in Chapter 15. It features some JavaScript APIs that are part of the Silverlight features but provide advanced possibilities. To be exact, Silverlight exposes a JavaScript API that lets developers download resources. While these resources are being downloaded, you can see the download progress and can process the downloaded data.

Dynamically Downloading Content

The technical background for Silverlight’s downloader API is the XMLHttpRequest JavaScript object, which fuels everything Ajax. The Silverlight API does not copy the XMLHttpRequest API, but provides its own interfaces. You can trigger the API from both XAML code-behind and HTML code-behind JavaScript code, but generally you will want to start from XAML.

When downloading content from within Silverlight, you usually have to follow these steps:

  1. Initialize the downloader object by using the plug-in’s createObject() method.

  2. Add event listeners to the object to react to important events.

  3. Configure the downloader object by providing information about the resource to download (HTTP verb, URL).

  4. Send the HTTP request.

  5. Handle any occurring events.

We will go through these step by step. But first we need some XAML backing so that we can wire up the code. Example 16-1 shows a suggestion.

Example 16-1. Downloading ...

Get Essential Silverlight 2 Up-to-Date now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.