文本无法从MediaPlayerElement(UWP)更新onTransportControl

时间:2019-08-13 17:06:32

标签: c# xaml uwp win-universal-app windows-10-universal

我尝试将视频标题放在这些代码的MediaPlayerElement的TransportControl上

MediaTransportControlsStyle.xaml

...
 <TextBlock
    Name="tblTitle"
    Grid.Column="1"
    Margin="5"
    FontFamily="Arial"
    FontSize="22"
    FontWeight="SemiBold"
    TextAlignment="Center" />
...

PlayerPage.xaml.cs

private async void MediaPlayer_MediaOpened(MediaPlayer sender, object args)
{
    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
        FrameworkElement transportControlsTemplateRoot = (FrameworkElement)VisualTreeHelper.GetChild(player.TransportControls, 0);
        TextBlock tblTitle = (TextBlock)transportControlsTemplateRoot.FindName("tblTitle");
        if (tblTitle != null)
            tblTitle.Text = MediaTitle; // Debug can run to this line
    });
}

问题是 tblTitle ,只是在首次应用程序更新时导航到 PlayerPage.xam l页面。从第二次 tblTitle 开始,即使调试运行到该行,也保持空白

tblTitle.Text = MediaTitle; // Debug can run to this line

我想我与Dispatcher.RunAsync方法有关,以更新UI线程。我尝试了几种方法,但是没有运气。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我发现转到PlayerPage时没有重新初始化MediaPlayer。每次都只需初始化就可以完成