导航到其他页面后继续播放的全局 MediaElement [英] Global MediaElement that continues playing after navigating to other page

查看:24
本文介绍了导航到其他页面后继续播放的全局 MediaElement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MediaElement 在我的 Metro 应用中播放音乐.我希望即使我导航到另一个页面,音乐也能继续播放.

I am using a MediaElement to play music in my metro app. I want the Music keeps playing even if I navigate to another Page.

在下面的主题中也有人问了这个问题:http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/241ba3b4-3e2a-4f9b-a704-87c7b1be7988/

In the following Thread that question was asked also: http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/241ba3b4-3e2a-4f9b-a704-87c7b1be7988/

我按照 JimMan 的建议做了1) 在 App.xaml.cs 中更改了根框架的控件模板以包含 MediaElement

I did what JimMan suggested 1) In App.xaml.cs Changed the control template of the root frame to include the MediaElement

var rootFrame = new Frame();
rootFrame.Style = Resources["RootFrameStyle"] as Style;
rootFrame.Navigate(typeof(HomePage), MainViewModel.Instance);
Window.Current.Content = rootFrame;
Window.Current.Activate();

2) 在 Styles.xaml 中添加

2) In Styles.xaml add

<Style  x:Key="RootFrameStyle" TargetType="Frame">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Frame">
                <Grid>
                    <MediaElement x:Name="MediaPlayer" AudioCategory="BackgroundCapableMedia" AutoPlay="True"  />
                    <ContentPresenter />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>   
</Style>

3) 要访问导航到的页面上的 MediaElement:

3) To Access the MediaElement on the Page navigated to:

DendencyObject rootGrid = VisualTreeHelper.GetChild(Window.Current.Content, 0);     
MediaElement rootMediaElement = (MediaElement)VisualTreeHelper.GetChild(rootGrid, 0);

但是 VisualTreeHelper.GetChild(Window.Current.Content, 0);始终返回 null,即使我尝试访问根页面上的 MediaElemt.

But VisualTreeHelper.GetChild(Window.Current.Content, 0); always returns null, even if I try to access the MediaElemt on the Root page.

我构建了一个小示例项目来演示.

I builded a little example Project to demonstrate.

示例项目

有什么想法吗?提前致谢!

Any Ideas ? Thanks in advance !

最好的问候法比安

推荐答案

在可视化树完全加载(添加到可视化树)之前,您尝试获取可视化树子级的 Navigated 处理程序可能会被调用.您可以尝试将代码移至 Loaded 事件处理程序.

It's possible your Navigated handler where you try to get the visual tree child gets called before the visual tree is fully loaded (added to visual tree). You could try moving your code to the Loaded event handler.

编辑*

我通过进行以下更改证实了我的理论:

I confirmed my theory by making the following change:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        this.Loaded += OnLoaded;
    }

    private void OnLoaded(object sender, RoutedEventArgs e)
    {
        DependencyObject rootGrid = VisualTreeHelper.GetChild(Window.Current.Content, 0);
        MediaElement rootMediaElement = (MediaElement)VisualTreeHelper.GetChild(rootGrid, 0);
    }
}

这篇关于导航到其他页面后继续播放的全局 MediaElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆