MediaElement.play() from within ViewModel

前端 未结 3 2129
我寻月下人不归
我寻月下人不归 2021-02-09 21:45

I\'m struggling with the following issue:

I am building a WP8 application using the MVVM patern. I have a media element on my view.xaml and the logic to control this med

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-09 21:56

    The answers above make use of MediaElement in the ViewModel. This element is a View-side component the VM should remain agnostic of.

    One way to implement this would be exposing an event in your ViewModel via an interface that exposes that event, then have the view respond to that event by running its MediaElement, via code-behind, or perhaps using interactivity triggers.

    ViewModel:

    public interface ISoundPlayer
    {
      event Action Play();
    }    
    
    public class MyViewModel : ViewModelBase, ISoundPlayer
    {
      public event Action Play;
    }
    

    View:

    public class MyView : UserControl
    {
      ISoundPlayer _SoundPlayer;
    
      public MyView()
      {
        DataContextChanged += OnDataContextChanged;
        Unloaded += OnUnloaded;
      }  
    
      void OnDataContextChanged(DependencyObject sender, DataContextChangedEventArgs args)
      {
        if (DataContext is ISoundPlayer player && _SoundPlayer != player)
        {
          if (_SoundPlayer != null)
            _SoundPlayer.Play -= OnPlay;
    
          _SoundPlayer = player;
          _SoundPlayer.Play += OnPlay;
        }    
      }
    
      void OnUnloaded(object sender, RoutedEventArgs e)
      {
        if (_SoundPlayer != null)
          _Metronome.Play -= OnPlay;
      }
    
      void OnPlay() => myMediaElement.Play();
    }
    

提交回复
热议问题