如何在UWP MediaPlayerElement中显示视频标题?

时间:2017-07-23 01:19:12

标签: c# xaml uwp media-player

我想知道如何使用媒体传输控件在屏幕的顶部显示视频标题。我在UWP中使用MediaPlayerElement,目标版本为14393。

1 个答案:

答案 0 :(得分:1)

MediaTransportControls不提供Title属性来显示视频标题。但是,我们可以轻松实现此功能,请参阅Create custom transport controls

由于我们想要向控件添加一个函数,我们需要创建一个派生自MediaTransportControls的新类。有关详细教程,请参阅Customize the transport controls下的创建衍生控件。有关完整示例,请参阅Media Transport Controls sample

在这里,您希望标题显示在屏幕的顶部,仅显示何时 弹出媒体传输控件,您可以在TextBlock已命名的" ControlPanel_ControlPanelVisibilityStates_Border "下添加Border。并将其VerticalAlignment设置为Top,如下所示:

<Border x:Name="ControlPanel_ControlPanelVisibilityStates_Border">
    <Grid>
        <TextBlock VerticalAlignment="Top" Foreground="Red" FontSize="36" Text="{TemplateBinding Title}" />
        <Grid x:Name="ControlPanelGrid" ...>
    </Grid>
</Border> 

在代码隐藏中,您可以为标题设置实现依赖项属性。

public sealed class CustomMediaTransportControls : MediaTransportControls
{
    public string Title
    {
        get { return (string)GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.Register("Title", typeof(string), typeof(CustomMediaTransportControls), new PropertyMetadata(null));

    public CustomMediaTransportControls()
    {
        this.DefaultStyleKey = typeof(CustomMediaTransportControls);
    }
    ...
}

在此之后,您应该能够使用自定义传输控件,如:

<MediaPlayerElement Name="MainMPE" AreTransportControlsEnabled="True" Source="video.mp4">
    <MediaPlayerElement.TransportControls>
        <local:CustomMediaTransportControls x:Name="customMTC"
                                            Title="This is a title">
        </local:CustomMediaTransportControls>
    </MediaPlayerElement.TransportControls>
</MediaPlayerElement>