❔ Trigger MediaElement.play() the MVVM way?
I have a button that is bound to a command that I would additionally like to trigger a hidden mediaelement from the community toolkit to play when it is clicked, and my code is (attempting to) follow the MVVM patterns. I have the mediaelement defined in the view, how would I cause a button click to trigger it?
3 Replies
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Someone asked that exact question on SO: https://stackoverflow.com/questions/10631748/mvvm-pattern-violation-mediaelement-play
TL;DR you could raise an event
PlayRequested
in the VM and subscribe to the event in the View. There, you just call MediaElement.Play()
. The other way would be a bit more elaborate but actually "feels" how MVVM should be used.
Basically the second answer of the StackOverflow question. You wrap the MediaElement in an Interface, create a new UserControl with them and then use the UserControl.Stack Overflow
MVVM pattern violation: MediaElement.Play()
I understand that ViewModel shouldn't have any knowledge of View, but how can I call MediaElement.Play() method from ViewModel, other than having a reference to View (or directly to MediaElement) in
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.