Partager via


MediaAttributeCollection

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

Represents a collection of MediaAttribute objects.

Syntax Notes

See Remarks.

Managed Equivalent

A String/String constrained Dictionary<TKey, TValue>.

Remarks

Each MediaAttribute object in the collection represents a name/value pair that corresponds to an XML tag in the ASX file that the Source property of the MediaElement object is set to.

If the Source property of the MediaElement is not set, is set to a media file rather than an ASX file, or is checked prior to the MediaOpened event, the collection is empty. Do not attempt to script to this collection until the MediaOpened event is raised.

This collection has an Add method, but it is not usable. Generally you must treat the collection as a one-time copy of a read-only collection that reflects the ASX file, and you should not modify its contents. Other collection methods such as Remove are also not particularly useful because you would only be modifying a copy. The most useful method of this collection is the specialized GetItemByName method.

Technically, you can use MediaAttributeCollection in XAML and instantiate it as a XAML object element. However, MediaAttributeCollection instantiation in XAML does not have any practical application. The useful collection of MediaAttribute objects is contained in the metadata of an ASX source that is loaded by a MediaElement. You can get this collection as the Attributes property through scripting, and this collection is not valid until the MediaOpened event is raised. Specifying a MediaAttribute in XAML ahead of time as part of a MediaElement will only result in that entire Attributes value being erased and replaced as soon as a media source is loaded by the MediaElement.

Example

The following JavaScript example shows how to retrieve a MediaAttribute object by name from the Attributes property of a MediaElement object that has a Source property set to an ASX playlist file. The values of the Name and Value properties of the MediaAttribute are then displayed.

function onMediaOpened(sender, args) {
    // Create the variable to hold the MediaAttribute.
    var attribute;
   
    // Get the MediaAttribute that is named Title.
    try
    {
var attributesCollection = sender.Attributes;
attribute = attributesCollection.getItemByName("Title");
    }
    catch(errorObj)
    {
alert(errorObj.message);
    }
    
    // Display the Value of the MediaAttribute.
    if(attribute != null)
    {
      alert("The title of the track is: " + attribute.value);
    }
}