Partilhar via


DownloadProgressChanged (Downloader)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Occurs while content is being downloaded during a download request.

[token = ]downloaderObject.AddEventListener("DownloadProgressChanged", eventhandlerFunction)

Arguments

AddEventListener Parameters

downloaderObject

object

A specific Downloader object.

token

integer

A token that is returned from the function, which you can optionally retain as a variable. If you intend to call RemoveEventListener to remove the handler, you will need this token.

eventhandlerFunction

object

The name of your event handler function as it is defined in script. When used as an AddEventListener parameter, quotation marks around the function name are not required. (See the "Remarks" section.)

Event Handler Parameters

sender

object

The object that invoked the event.

eventArgs

object

This parameter is always set to null.

Managed Equivalent

None. Downloader does not exist in the managed API. Generally you can use WebClient for downloads, which provides equivalent events and functionality.

Remarks

The DownloadProgressChanged event can be used monitor the progress of a download request. The DownloadProgressChanged event occurs whenever the amount of total content downloaded increases by 0.05 or more (as a factor of 1.0, which indicates completion), or reaches 1.0. Either the Completed or DownloadFailed event will also occur when the state of the download request has changed.

You can also add handlers in script by using a quoted string for the event handler name, as follows:

downloaderObject.AddEventListener("DownloadProgressChanged", "eventhandlerFunction")

This syntax also returns a token. However, the token is not an absolute requirement for removing the handler in cases where the handler was added by using a quoted string. For details, see RemoveEventListener.

The progress factor is not carried in event data. Instead it is available as a property of the Downloader object that fired the event. That object is always the sender of the event.

Example

The following JavaScript example shows how to use the DownloadProgress (Downloader) property in a DownloadProgressChanged event handler function:

// Event handler for updating visual progress indicator.
function onDownloadProgressChanged(sender, eventArgs)
{
    // Calculate the downloaded percentage.
    var percentage = Math.floor(sender.downloadProgress * 100);

    // Update the Rectangle and TextBlock objects of the visual progress indicator.
    progressText.text = percentage + "%";
    progressRectangle.width = percentage * 2; 
}

The following illustration shows the visual progress indicator at the starting, middle, and ending position of the download.

Visual progress indicator

Progress indicator

A visual progress indicator can be constructed from a wide variety of XAML content, including animated objects.

Applies To

Downloader