SetSource Method (Image)

Sets the source value of an object with downloaded content.

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

Parameters

downloader

Downloader

The object containing the downloaded content.

part

string

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.

Remarks

The SetSource method sets the downloaded image content to the Source property of the Image. Equivalent SetSource methods exist on ImageBrush (for ImageSource) and MediaElement (for Source). 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.

Examples

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

JavaScript
// Event handler for the Completed event.
function onCompleted(sender, eventArgs)
{
    // Retrieve the Image object.
    var myImage = sender.findName("myImage");
    // Set the Source property to the contents of the downloaded object,
    // In this case, since the downloaded content represents a single image file, promo.png,
    // the part parameter is set to an empty string.
    myImage.setSource(sender, "");
}

Silverlight provides the ability to download content as a package, which is a collection of independent files containing XAML content, media assets, and other application data. The Zip file format is supported as a download package. The following illustration depicts a collection of application content contained within a Zip download package.

Download package contains a collection of application files

Download package contains a collection of application files

Once the package is successfully downloaded, you can use methods, such as GetResponseText, SetSource, and CreateFromXamlDownloader, to selectively retrieve a specific named part of the package. In this case, a part reference is the filename reference of the individual content within the package, such as "rotation01_green.png".

Retrieving a part from a download package

Retrieving a part from a download package

You can use the SetSource method to retrieve a specific part within the downloaded content package. When the downloaded content package is a Zip file, the SetSource method allows you to retrieve the contents of a filename within the Zip file. The following JavaScript example shows how to use the SetSource method to set the Source property of an Image object to a specified part within the downloaded content:

JavaScript
function onDownloadCompleted(sender, eventArgs)
{
    // Retrieve the XAML content from the downloaded package file.
    var jacketBrowserXaml = sender.getResponseText("jacketBrowser.xaml");
    // Create the objects from the XAML content.
    var jacketBrowser = plugin.content.createFromXaml(jacketBrowserXaml);
    // Add downloaded XAML content to the root Canvas of the plug-in.
    sender.findName("root").children.insert(0, jacketBrowser);
    // Retrieve a reference to the Image object representing the jacket.
    var jacketImageSlice = sender.findName("jacketSlice");
    // Set the Source property of the Image object to the specific jacket image
    // within the downloaded Zip package file.
    jacketImageSlice.setSource(sender, "rotation01_green.png");
}

Applies To

Image

See Also

Using a Downloader Object
Downloader
SetSource Method (ImageBrush)
SetSource Method (MediaElement)