Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>One way to accomplish this in the MVVM style is to use the MVVM Light Messenger class to send play/pause messages from the viewmodel, which the view would subscribe to and receive.</p> <p>Say you have a pair of Buttons on your view for Play and Pause. You would drop an <a href="http://jesseliberty.com/2011/01/05/windows-phone-from-scratch-mvvm-light-toolkit-soup-to-nuts-part-2/" rel="nofollow">EventToCommand behavior</a> on each of them in Expression Blend, and then you would bind them to a pair of RelayCommand properties in your viewmodel, one RelayCommand for Play, one for Pause. For example, in your viewmodel, you would have:</p> <pre><code>public RelayCommand PlayCommand { get; private set; } public RelayCommand PauseCommand { get; private set; } </code></pre> <p>The Play command would have the Messenger send a Play message, and the Pause command would send a Pause message. Each message would be its own simple class:</p> <pre><code>public class PlayMessage { } public class PauseMessage { } </code></pre> <p>Then, in the constructor of your viewmodel, you would create new RelayCommands for the two RelayCommand properties you created earlier that would have actions that have the Messenger send the messages:</p> <pre><code>MyViewModel() { PlayCommand = new RelayCommand( () =&gt; SendPlayMessage() ); PauseCommand = new RelayCommand( () =&gt; SendPauseMessage() ); } private void SendPlayMessage() { Messenger.Default.Send&lt;PlayMessage&gt;( new PlayMessage() ); } private void SendPauseMessage() { Messenger.Default.Send&lt;PauseMessage&gt;( new PauseMessage() ); } </code></pre> <p>Your view would then <a href="http://jesseliberty.com/2011/01/06/windows-phone-from-scratch%E2%80%93mvvm-light-toolkit-soup-to-nuts-3/" rel="nofollow">have the Messenger subscribe</a> to both of these message types, and would have actions that would call the Play and Pause methods on the MediaElement:</p> <pre><code>MyView() { Messenger.Default.Register&lt;PlayMessage&gt; ( this, ( action ) =&gt; ReceivePlayMessage( action ) ); Messenger.Default.Register&lt;PauseMessage&gt; ( this, ( action ) =&gt; ReceivePauseMessage( action ); ); } private void ReceivePlayMessage(PlayMessage msg) { myMediaElement.Play(); } private void ReceivePauseMessage(PauseMessage msg) { myMediaElement.Pause(); } </code></pre> <p>Where myMediaElement is the name you give to the MediaElement in your view's xaml.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload