我有一个要在 8.1 Windows 商店应用程序中播放的视频,并且想在导航到其他页面后继续播放音频.. 我使用 visualTreeHelper 做到了,所以我在 app.xaml 中声明了一个媒体元素并将其添加到 app.xaml.cs 的框架中,并在 playPage 中获取。问题是媒体元素控件只播放音频,我看不到视频..导航后音频继续播放,但在播放页面中看不到视频(只有音频):所以这就是我输入的标准样式.xaml:
<Style x:Key="RootFrameStyle" TargetType="Frame">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Frame">
<Grid>
<MediaElement x:Name="player" AudioCategory="BackgroundCapableMedia" />
<Grid>
<ContentPresenter />
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这在 app.xaml.cs
rootFrame.Style = Resources["RootFrameStyle"] as Style;
在playPage.xaml 我添加MediaElement Control:
<ContentControl x:Name="videoContainer" HorizontalAlignment="Stretch" VerticalAlignment="Center"
Grid.Row="0" Grid.Column="1"
KeyUp="VideoContainer_KeyUp" >
<MediaElement x:Name="player" AudioCategory="BackgroundCapableMedia"
Visibility="Visible" Grid.Row="0" Grid.Column="1" AutoPlay="True"
HorizontalAlignment="Center" VerticalAlignment="Center"
MediaOpened="player_Opened"
MediaEnded="player_Ended"
MediaFailed="player_Failed"
Position="10"
CurrentStateChanged="player_CurrentStateChanged" />
</ContentControl>
并在其背后的代码中:
DependencyObject rootGrid = VisualTreeHelper.GetChild(Window.Current.Content, 0);
player = (MediaElement)VisualTreeHelper.GetChild(rootGrid, 0) as MediaElement;
player.Source = video.VideoLink;
`
当我在导航后不尝试让音频工作时一切正常,所以当我不使用视觉树助手但在这种情况下音频按预期工作但在播放页面中看不到视频(只有音频)