1

I am working on an MVVM C# Metro application that uses media components, particularly leveraging the play to capabilities.

Normally it seems that you would bind properties, however I need to make calls such as MediaElement.Play(source); and things such as that. The best solution I have come up with thus far is to fire an event from the view model that is handled by the code behind.

Is this in fact the best practice, or is there a more sophisticated approach?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Dev Dave
  • 41
  • 2

2 Answers2

0

There is an useful series of articles on MSDN that might help you to do this in more efficient way:

NOTE: These are still .Net 4 examples, but I bet that it will not require a lot efforts to run it with Windows 8 with all its enhancements.

Anatolii Gabuza
  • 6,184
  • 2
  • 36
  • 54
0

I have tried doing a media player in WPF using MVVM way once, what I felt from my experience is that, it will be a real pain in doing it in the MVVM way. I would suggest write code behind if its less complex and faster than sticking to MVVM always, You can separate it as a user control later with some dependency properties if you want it to look cleaner. Anyways, regarding the media playback what you could "also" do is keep a media player (Media Player) in your viewmodel and create a videobrush pointing to that mediaplayer and use it to show your video in the view. Rectangle or any other element for which you set the drawing brush can be used. As your media player is the viewmodel, now you can play it, stop it, seek it etc

Something like this,

        var player = new MediaPlayer();
        var myVideo = new VideoDrawing { Rect = new Rect(0, 0, 1, 1), Player = player  };
        var dBrush = new DrawingBrush(myVideo);

        // Use drawing brush to fill a rectangle

        rectangle.Fill = dBrush;
Moble Joseph
  • 647
  • 4
  • 14