共用方式為


MediaElement.Source Property

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

Gets or sets a media source on the MediaElement.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public Property Source As Uri
public Uri Source { get; set; }
<MediaElement Source="uri" .../>

Property Value

Type: System.Uri
A string that specifies the source of the element, as a Uniform Resource Identifier (URI). The default value is nulla null reference (Nothing in Visual Basic).

Remarks

Dependency property identifier field: SourceProperty

Setting the Source will reset the media's Position to 00:00:00.

The string provided for the Source property is interpreted as a Uniform Resource Identifier (URI). This is in contrast to an Internationalized Resource Identifier (IRI). This distinction means that characters used to identify a Source that are outside of US-ASCII will need to be encoded.

Relative references are permitted. For more information on how relative references are evaluated, see Resource Files.

Cross-domain URIs are permitted (using http). You can also use the monikers mms:, rtsp:, or rtspt: in your URI to point to a media source. In each case the actual protocol and URI scheme falls back to http (with possible further fallbacks in behavior for differentiating streaming from progressive downloads), but the value of Source will retain your moniker regardless of the runtime resolution of the URI.

By using the SetSource method instead of setting Source directly, you can download content as a package and access parts from within the package. For details on downloading resources such as media as streams, with ability to access parts of a package, see Downloading Content on Demand.

You can specify an initial Source value of "" (empty string) in your XAML MediaElement object element, and then set the value in code at a later time. This creates a non-playing placeholder MediaElement that you could later set a specific Source for in response to a user choice.

The MediaFailed event can occur if the initial Source attribute value in XAML does not specify a valid source.

Setting a cross-domain URI for Source will either succeed or will not return a valid status for any failure. In this case, a MediaFailed event does not occur.

Backslashes (\) in Source URIs are not permitted; always use forward slashes (/).

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.