One way to achieve this in the MVVM style is to use the MVVM Light Messenger class to send play / pause messages from a view that will subscribe and receive.
Say you have a couple of buttons on your screen for Play and Pause. You would throw the EventToCommand behavior on each of them into Expression Blend, and then bind them to the RelayCommand property pairs in your view model, one RelayCommand for Play, one for Pause. For example, in your view model, you would have:
public RelayCommand PlayCommand { get; private set; } public RelayCommand PauseCommand { get; private set; }
In the Play command, Messenger will send a Play message, and the Pause command will send a Pause message. Each message will be its own simple class:
public class PlayMessage { } public class PauseMessage { }
Then, in the constructor of your view model, you will create new RelayCommands for the two previously created RelayCommand properties, which will have actions in which Messenger sends messages:
MyViewModel() { PlayCommand = new RelayCommand( () => SendPlayMessage() ); PauseCommand = new RelayCommand( () => SendPauseMessage() ); } private void SendPlayMessage() { Messenger.Default.Send<PlayMessage>( new PlayMessage() ); } private void SendPauseMessage() { Messenger.Default.Send<PauseMessage>( new PauseMessage() ); }
Your view will then inform Messenger subscribers of both of these message types and will have actions that will invoke the Play and Pause methods on the MediaElement:
MyView() { Messenger.Default.Register<PlayMessage> ( this, ( action ) => ReceivePlayMessage( action ) ); Messenger.Default.Register<PauseMessage> ( this, ( action ) => ReceivePauseMessage( action ); ); } private void ReceivePlayMessage(PlayMessage msg) { myMediaElement.Play(); } private void ReceivePauseMessage(PauseMessage msg) { myMediaElement.Pause(); }
Where myMediaElement is the name you pass MediaElement in your xaml view.