Condividi tramite


SetSource (Image)

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

Sets the source value of an Image object with downloaded content.

object.SetSource(downloader, part)

Arguments

downloader

Downloader

The Downloader object that contains 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 file name in downloader. If the downloaded content does not represent packaged content, set part to an empty string.

Managed Equivalent

BitmapSource.SetSource - use a stream obtained from WebClient as input.

Remarks

The SetSource method sets the downloaded image content to the Source property of an Image object. Equivalent SetSource methods exist on ImageBrush (for ImageSource) and MediaElement (for Source). The first parameter, downloader, identifies the Downloader object that represents the downloaded content. The second parameter, part, identifies the specific part to retrieve from 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.

Example

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 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, because 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 that contain XAML content, media assets, and other application data. The .zip file format is supported as a download package.

When the package has been successfully downloaded, you can use methods such as GetResponseText, SetSource, and CreateFromXamlDownloader to selectively retrieve a named part of the package. When the downloaded content package is a .zip file, the SetSource method lets you retrieve the contents of a file name from within the .zip file archive.

The following JavaScript example shows how to use the SetSource method to set the Source property of an Image object to a specified part ("rotation01_green.png") of the downloaded content.

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