文本未从 MediaPlayerElement (UWP) 更新 onTransportControl
Text not update onTransportControl from MediaPlayerElement (UWP)
我尝试像这些代码一样将视频标题放在 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.xaml 页面。从第二次开始 tblTitle 即使在调试 运行 行
时也保持空白
tblTitle.Text = MediaTitle; // Debug can run to this line
我想我与 Dispatcher.RunAsync 方法有关,可以更新 UI 线程。我尝试了几种方法但没有运气。我该如何解决这个问题?
我发现我在进入PlayerPage时没有重新初始化MediaPlayer。只需每次初始化即可完成
我尝试像这些代码一样将视频标题放在 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.xaml 页面。从第二次开始 tblTitle 即使在调试 运行 行
时也保持空白tblTitle.Text = MediaTitle; // Debug can run to this line
我想我与 Dispatcher.RunAsync 方法有关,可以更新 UI 线程。我尝试了几种方法但没有运气。我该如何解决这个问题?
我发现我在进入PlayerPage时没有重新初始化MediaPlayer。只需每次初始化即可完成