4

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.

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

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) 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) To Access the MediaElement on the Page navigated to:

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

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.

Sample Project

Any Ideas ? Thanks in advance !

Best regards Fabian

Filip Skakun
  • 31,624
  • 6
  • 74
  • 100
elDietze
  • 43
  • 4

1 Answers1

2

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.

EDIT*

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);
    }
}
Filip Skakun
  • 31,624
  • 6
  • 74
  • 100
  • No the result ist the same, for me it Looks like the MediaElement isn't placed properly in the root Frame. – elDietze Jun 09 '12 at 13:32
  • It did work fine for me. (note, I am using Release Preview, though that should not really matter) – Filip Skakun Jun 09 '12 at 15:44
  • Thank you very much ! It works now. Your next beer (even if only virtual) is on me ! ![Beer](http://images.derberater.de/files/imagecache/456xXXX_berater/berater/slides/Bier-erfunden.jpg). Greetings Fabian – elDietze Jun 10 '12 at 09:32
  • I managed to get this to work and have my player play music in the background even if I switch frames, but how do I handle the OS events like Play, Pause, Next Track etc from the media keys? Do I have to add duplicate event handlers to each View? or is there some way to handle these globally in the application? – SelAromDotNet Jan 04 '13 at 04:15
  • That sounds like totally separate question. :) – Filip Skakun Jan 04 '13 at 07:18
  • well, it's related, because I need to tell the mediaelement to respond to the events, and I'm not sure how they work together... I'll keep at it and if I figure out something specific to ask I'll open a new question, thanks – SelAromDotNet Jan 05 '13 at 05:25