1

I want to play songs in my windows-store app. the thing is, I want to "share" the MediaElement between pages because playing the sound should not stop when navigating to an other page. I am using the newest Caliburn.Micro WinRT port.

My first approach to this problem was to have the MediaElement on the DefaultView where i have a grid with 2 rows. 2nd row contains the MediaElement (and all the controls that handle play, pause and so on) and 1st row is another frame where I want to inject the actual content.

For that to work, I needed some hacking. I created a new interface

public interface INavigationService2 : INavigationService { }

and my own NavigationService as

public class NavigationService : FrameAdapter, INavigationService2
{
    public NavigationService(Frame frame, bool treatViewAsLoaded = false) 
        : base(frame, treatViewAsLoaded) { }        
}

now, I registered that with CM

public sealed partial class MainView 
{
    public MainView()
    {
        this.InitializeComponent();
        MainView.InitializeComponentStatic(this);    
    }

    private static bool _isInitialized;
    private static void InitializeComponentStatic(MainView mainView)
    {
        if (_isInitialized) return;

        ((App)Application.Current).RegisterInstance(typeof(INavigationService2),
             null, new NavigationService(mainView.ContentFrame));
        _isInitialized = true; 
    }
}

then i tried to bind the my StartViewModel to that ContentFrame doing

public class MainViewModel : Screen
{
    public MainViewModel()
    {
        this.ContentFrame = new StartViewModel(((App) Application.Current).GetDefaultInstance<INavigationService2>());
    }

    public DefaultViewModel ContentFrame { get; set; }
}

when I run this, the StartView is shown inside the MainView Grid, but navigation does not longer navigate to the right view/viewmodel (it actually loads the MainViewModel again somehow).

Since this is not really nice and straight forward, I wanted your ideas on how to achieve the original problem: sharing the mediaelement between pages/views.

esskar
  • 10,638
  • 3
  • 36
  • 57

1 Answers1

1

Instead of using MediaElement, you might want to consider using XAudio2. Here is a walkthrough of how to get it done: Playing of background music/sound effects in Windows Store Apps (C# XAML)

VT Chiew
  • 663
  • 5
  • 19
  • it doesn't support mp3, does it? – esskar Oct 25 '12 at 11:04
  • 1
    i'm afraid not, sorry for missing your comment out. I should have replied earlier. – VT Chiew Nov 29 '12 at 15:36
  • no worries, as i commented above, i found a solution! thanks. – esskar Nov 29 '12 at 22:04
  • @esskar Sorry for late comment, but could you please say how you accomplished this? – Alexei Malashkevich Apr 28 '13 at 18:50
  • @AlexeiMalashkevich check my last comment on the original post. the basic idea is to share the same media control over all pages by adding it to the application frame. – esskar Apr 29 '13 at 08:47
  • @esskar yes, I uderstand that singletone is best solution for this, but I still not sure how to do this with MediaElement. One possible solution - use Shell which contains MediaElement and Container for other usercontols, but may be this is not best decision. – Alexei Malashkevich Apr 29 '13 at 09:37