我在 StandardStyles.xaml 中有下一个代码:
<Style x:Key="RootFrameStyle" TargetType="Frame">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Frame">
<Grid>
<MediaElement x:Name="MediaPlayer" AudioCategory="BackgroundCapableMedia" />
<ContentPresenter />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
在 App.Xaml.cs 中 OnLaunched 中的下一个代码:
...
rootFrame.Style = Resources["RootFrameStyle"] as Style;
...
在 MainPage.xaml.cs 代码中:
PlayToManager playToManager = null;
CoreDispatcher dispatcher = null;
private void LayoutAwarePage_Loaded_1(object sender, RoutedEventArgs e)
{
if (App.Player == null)
{
var rootGrid = VisualTreeHelper.GetChild(Window.Current.Content, 0);
App.Player = (MediaElement)VisualTreeHelper.GetChild(rootGrid, 0);
App.Player.Source = new Uri("http://myserverlink.com/mymp3file.mp3");
App.Player.Play();
}
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
dispatcher = Window.Current.CoreWindow.Dispatcher;
playToManager = PlayToManager.GetForCurrentView();
playToManager.SourceRequested += playToManager_SourceRequested;
}
void playToManager_SourceRequested(PlayToManager sender, PlayToSourceRequestedEventArgs args)
{
var deferral = args.SourceRequest.GetDeferral();
var handler = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
args.SourceRequest.SetSource(App.Player.PlayToSource);
deferral.Complete();
});
}
如果我在 Windows 8 中单击设备,则在 PlayTo 的设备列表中没有设备。如果要在 MainPage.xaml 中添加一个 MediaElement,则列表中的设备。但是我需要MediaElement 的Style File,导航到另一个页面没有停止播放音乐。该怎么办?