How to play sound and animation in MVVM

I read a lot of blogs about the best way to play sound / animation, but if possible, I would like to see a simplified example of how this is done so that I better understand.

So I understand in MVVM

View → Sound and Animation

ViewModel -> If some value is true, I would like to play sound and animation in the view.

Now, how will I do this. I was told to use interfaces like ISoundService and IAnimationService. Implement in a view and then do what? If possible, a useful example of bare bone will help a lot.

+3
source share
2 answers

, View, . , - :

public interface IAudioPlayer
{
    void Play(string fileName);
}

public class AudioPlayer : IAudioPlayer
{
    private readonly SoundPlayer player = new SoundPlayer();

    public void Play(string fileName)
    {
        player.Stream = File.OpenRead(fileName);
        player.Play();
    }
}

Injection Dependency, ViewModel:

public class TheViewModel
{
    public TheViewModel(IAudioPlayer audioPlayer)
    {
         // probably store it as a private readonly field for later use.
    }
}

, , , ViewModel ... , EventAggregator.

, . XAML. - , ViewModel, View .

, , ViewModel, - , .

, , MVVM/MVP, ViewModel IView , ExecuteDeletionAnimation. ViewModel , .

, ?

+8

, ...

→ , /

ViewModel →

WPF, , , UIElements . , MVVM , . MVVM MVC, .

+1

Source: https://habr.com/ru/post/1748081/


All Articles