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

How to show video title in UWP MediaPlayerElement?

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

MediaTransportControls do not provide a Title property to show video title. However, we can implement this easily as refer to Create custom transport controls.

因为我们想给控件添加一个函数,我们需要创建一个新的 class 派生自 MediaTransportControls. For a detailed tutorial, please see Create a derived control under Customize the transport controls. And for a complete sample, please refer to Media Transport Controls sample.

在这里,如你所愿,标题显示在屏幕的顶部,并且仅在 弹出媒体传输控件,可以在名为“ControlPanel_ControlPanelVisibilityStates_Border”的Border下添加TextBlock并将其 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> 

而在code-behind中,你可以为标题的设置实现一个依赖属性。

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>