Silverlight has a Downloader object that can be used to download the corresponding files in the Silverlight application, such as scripts, media files, and so on. With it, you can download the appropriate files based on the specific needs of your application without having to download all the files needed by the entire application when the Silverlight plug-in is initialized. This not only saves the time the user spends waiting to download the application, but also dynamically uses the downloaded content directly in the application without refreshing the entire page.
The Downloader object can initialize data transmission, monitor the process of data transfer, and can easily get the downloaded files. The implementation of the progress bar when loading an application requires the powerful functionality of the Downloader object. In addition, the Downloader object can be used to download the ZIP compression package and obtain a single file from the downloaded Zip package.
To use the Downloader object, you first create a Downloader object instance in the Silverlight plug-in using the CreateObject method of Silverlight. There are several common methods in downloader:
The Open method is used to instantiate the Downloader object and to specify what to download. This method contains two arguments of type string: The first parameter specifies the behavior of the download, only "Get" is currently supported, and the second parameter is a URI that indicates the downloaded file.
The Send method is used to perform a download request. The method does not contain any arguments and downloads the file by using an asynchronous method.
Because the Downloader object downloads files using asynchronous methods, the file operations after downloading are generally performed through event-triggered methods. You can use the AddEventListener method to add event sniffing for Downloader object instances. The AddEventListener method contains two parameters: the first parameter indicates the Downloader event to be listened to, and the second parameter indicates the method name of the event handling. Downloader objects A total of three events:
The completed event is triggered when the download request completes and returns the contents of the download.
The DownloadFailed event is returned when the download fails.
The DownloadProgressChanged event is triggered during the download process.
An example of using a Downloader object is as follows:
function onDemandLoaded(sender, eventArgs)
...{
//首先得到当前Silverlight 插件的引用
var SilverlightPlugin = sender.getHost();
//创建一个Downloader对象
var downloader = SilverlightPlugin.createObject("downloader");
//初始化 Downloader对象
downloader.open("GET", "myPage.xaml");
//执行下载请求
downloader.send();
//添加事件侦听
downloader.addEventListener ("downloadProgressChanged", onDownloadProgressChanged);
downloader.addEventListener ("completed", onCompleted);
}
function onDownloadProgressChanged(sender, eventArgs)
...{
//sender返回的即是已下载的文件对象
}
function onCompleted(sender, eventArgs)
...{
}