This question is not related to the MediaElement
—pertaining to this, I’ve read both MediaElement.play() from within ViewModel and MVVM pattern violation: MediaElement.Play(), and I agree with both of them.
But say that we’re using instead a third-party assembly such as NAudio. We don’t have to find a way of plugging an existing XAML media control’s interactive features into our ViewModel. Instead, we can simply use, e.g., InteractionTrigger
s in our View, subscribe to it in our ViewModel, and invoke the play command on a given media file.
Now my question is: is this still something that fits, within the MVVM pattern, into the ViewModel? Or should we rather hand this down to the Model and play the file from there?
I would say it entirely depends on the nature of the media file and its relation to your app's core logic.
Is it just a UI feedback sound effect that gets played when you click on something? Stays in the view.
Is it a music player app that plays music from a playlist? That's core logic and belongs in the VM.
If I'm unsure where something lives, I think about an imaginary "view" on my app that uses a text console rather than WPF. If the logic still exists even if driven by a console, then it lives in the VM.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With