如何从 ViewModel (MVVM Light Toolkit) 访问视图中的 UserControl (MediaElement)
我对 MVVM Light Toolkit(以及 WPF)非常陌生。
我遇到以下问题,但不知道如何解决它:
在我的 MainView 中,我放置了一个 MediaElement 和一些按钮,它们控制应用程序的行为。我将带有 RelayCommands 的按钮连接到 ViewModel,因此已经可以正常工作了。我发现,通过 MainView 的 CodeBehind 文件启动和停止 MediaElement 很容易,但我喜欢通过 RelayCommands 从 ViewModel 内控制 MediaElement。所以我想将对 MedieElement 的引用注入到 ViewModel 中。我发现 ViewModelLocator 首先被构造,这将构造 ViewModel,最后构造 MainView,因此在这种情况下注入将不起作用。
那么,让 ViewModel 接收对 MediaElement 的引用的最佳解决方案是什么?消息?
最好的问候
Bernhard
ps:我也在 mvvm light 工具包主页(codeplex)的讨论小组中问了这个问题
I am very new to the MVVM Light Toolkit (and also to WPF).
I have the following problem, and don't see how to solve it:
In my MainView I place a MediaElement, and also some buttons, which control the behavior of the application. I wired the button with RelayCommands to the ViewModel, so thats already working fine. I figured out, that it is easy to start and stop the MediaElement via a CodeBehind file of the MainView, but I like to control the MediaElement from within the ViewModel via the RelayCommands. So I thought to inject a reference to the MedieElement in to the ViewModel. As I find out the ViewModelLocator is contructed first, and this will contruct the ViewModel and at last the MainView is constructed, so an injection won't work in this case.
So, what would be the best solution to let the ViewModel receive a reference to the MediaElement? Messages?
best regards
Bernhard
ps: I also asked this question at the discussion panel at mvvm light toolkit homepage (codeplex)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
但不要这样做。在 MVVM 中,您永远不应该引用 ViewModel 中的 UI 元素。相反,从 ViewModel 引发在代码隐藏中处理的事件来控制
MediaElement
Don't do that. In MVVM you should never have a reference to a UI element in the ViewModel. Instead, raise events from the ViewModel that are handled in the code-behind to control the
MediaElement