MediaElement.CurrentState 属性

定义

获取此 MediaElement 的状态。

public:
 property MediaElementState CurrentState { MediaElementState get(); };
MediaElementState CurrentState();
public MediaElementState CurrentState { get; }
var mediaElementState = mediaElement.currentState;
Public ReadOnly Property CurrentState As MediaElementState

属性值

MediaElement 的当前状态。 状态可以是 MediaElementState 枚举) 中定义的以下 (之一: 缓冲关闭打开暂停正在播放已停止。 默认值为 Closed

示例

以下示例演示显示 MediaElement 的 CurrentState 的一种方法。 它创建一个 MediaElement 和多个用于控制媒体播放的按钮。 为了显示 MediaElement 的当前状态,该示例注册 CurrentStateChanged 事件并使用事件处理程序更新 TextBlock

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <MediaElement CurrentStateChanged="Media_State_Changed"
        x:Name="media" Source="xbox.wmv" Width="300" Height="300" 
                  Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3" />

    <!-- Stops media playback.-->
    <Button Click="StopMedia" 
     Grid.Column="0" Grid.Row="1" Content="Stop" />

    <!-- Pauses media playback. -->
    <Button Click="PauseMedia" 
     Grid.Column="1" Grid.Row="1" Content="Pause" />

    <!-- Begins media playback. -->
    <Button Click="PlayMedia" 
     Grid.Column="2" Grid.Row="1" Content="Play" />

    <TextBlock
Grid.Column="0" Grid.Row="2" Margin="10" 
FontSize="12">CurrentState:</TextBlock>

    <TextBlock 
x:Name="mediaStateTextBlock"
Grid.Column="1" Grid.Row="2" Margin="0,10,0,0"
FontSize="12"></TextBlock>

</Grid>
private void StopMedia(object sender, RoutedEventArgs e)
{
    media.Stop();
}
private void PauseMedia(object sender, RoutedEventArgs e)
{
    media.Pause();
}
private void PlayMedia(object sender, RoutedEventArgs e)
{
    media.Play();
}
private void Media_State_Changed(object sender, RoutedEventArgs e)
{
    mediaStateTextBlock.Text = media.CurrentState.ToString();
}
Private Sub StopMedia(ByVal sender As Object, ByVal e As RoutedEventArgs)
    media.Stop()
End Sub

Private Sub PauseMedia(ByVal sender As Object, ByVal e As RoutedEventArgs)
    media.Pause()
End Sub

Private Sub PlayMedia(ByVal sender As Object, ByVal e As RoutedEventArgs)
    media.Play()
End Sub
Private Sub Media_State_Changed(ByVal sender As Object, ByVal e As EventArgs)
    mediaStateTextBlock.Text = media.CurrentState.ToString
End Sub

适用于