SetSource Method (MediaElement)

Sets the source value of an object with downloaded content.

Cannot use methods in XAML.
object.SetSource(downloader, part)




The object containing the downloaded content.



The name of the specific part of the downloaded content package. When the downloaded content package is a Zip file, part refers to the contents of a filename within downloader. If the downloaded content does not represent packaged content, set part to an empty string.


The SetSource method sets the downloaded media content to the Source property of the MediaElement. Equivalent SetSource methods exist on ImageBrush (for ImageSource) and Image (for Source).

SetSource differs notably from just setting Source in that cross-domain URIs for Source are permitted, whereas cross-domain URIs for the Downloader object you create to support SetSource are not permitted. Therefore, if you are sourcing media element content using cross-domain and/or referencing media using a specific moniker such as mms:, you should not use SetSource and should instead set Source directly.

The first parameter, downloader, identifies the Downloader object representing the downloaded content. The second parameter, part, identifies the specific part to retrieve within the downloaded content. If the downloaded content does not represent packaged content, such as a Zip file, part must be set to an empty string.

Note   In Silverlight 1.0, if an initial download attempt through SetSource fails (the underlying Downloader raises the DownloadFailed event), then subsequent attempts to call SetSource will also result in a failed download, even if the URI is expected to result in a successful download on subsequent attempts. In order to avoid this issue, you should provide a DownloadFailed handler on the underlying downloader. If DownloadFailed is detected, then you should null out the existing MediaElement, create a new one (using CreateFromXaml) and re-add it to the object tree at the same position. This will provide a MediaElement that will avoid the no-reset issue with its SetSource API.


The following JavaScript example shows how to use the SetSource method to set the Source property of an Image object to the downloaded content:

// Event handler for the Completed event.
function onCompletedVideo(sender, eventArgs)
    // Retrieve the MediaElement object.
    var myMediaElement = sender.findName("myMediaElement");
    // Set the Source property to the contents of the downloaded object,
    // named xbox.wmv as a zip part in the download
    myMediaElement.setSource(sender, "xbox.wmv");

Applies To


See Also

Using a Downloader Object
SetSource (Image)
SetSource (ImageBrush)