PRISM/MEF:2 个窗口如何才能实现?视图模型互相交谈?
基本上,我不知道从哪里开始:
我有 Shell.xaml 窗口。 我还有 Popup.xaml 窗口。
我将 Shell.xaml 设置为导入 PopupWindow,然后当 PopupWindow Loaded 事件触发时,它会执行以下操作:
Popup.Owner = this;
Popup.Show();
现在,我需要能够让 PopupWindow 的 ViewModel 与 Shell.xaml 进行通信。基本上,我需要能够让 PopupWindow 告诉 Shell 的 ViewModel 用户输入的信息。
更新:
为了保持这种解耦,我不想将客户端视图模型的任何实例传递给弹出窗口,我宁愿让弹出窗口的 ViewModel 以某种方式与客户端的 ViewModel 进行通信,而不知道它实际上是谁说话。
Basically, I'm not sure where to start:
I have my Shell.xaml window.
I also have my Popup.xaml window.
I set the Shell.xaml to import the PopupWindow then when the PopupWindow Loaded event fires, it does:
Popup.Owner = this;
Popup.Show();
Now, I need to be able to have the PopupWindow's ViewModel communicate with the Shell.xaml. Basically, I need to be able to have the PopupWindow tell the Shell's ViewModel information the user inputs.
Update:
In keeping this decoupled, I don't want to pass in any instance of the Client's viewmodel to the popup, I'd much rather have the Popup's ViewModel somehow have a way of communicating to the Client's ViewModel without knowing who it is actually talking to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看 Prism 中的事件聚合器。 Prism 中的聚合事件旨在作为促进解耦、视图模型间通信的一种手段。如果您想要“纯”MVVM,我认为它会是这样的:
Have a look at the event aggregator in Prism. The aggregated events in Prism are intended as a means of facilitating decoupled, inter-viewmodel communication. If you are going for "pure" MVVM, I think that it would go something like this:
我不是 PRISM/MEF 专家,但如果我要解决这个问题,我会采取稍微不同的方法并添加更多的解耦。本质上,您希望窗口(Shell 和 Popup)的 ViewModel 进行通信 - 窗口(视图)应该只与用户通信并更新属性(在解耦的模型中)绑定时尚)在他们的ViewModels上。
一旦您处于此位置,Shell 的 ViewModel 就可以请求 Popup 的 ViewModel 的用户信息(例如从属性)。当然,它们不是 Shell 或 Popup 的 ViewModels - 它们只是这些 View 碰巧绑定到的 ViewModels :) 纯粹
主义者会走得更远,我认为,谈论各个通信方之间的消息队列,但一次一步。
Dan
编辑
按照 Michael 的评论:
正如我所说,我不是 PRISM 专家,但我认为这取决于您想要解耦到什么程度。没有什么可以阻止客户端 ViewModel 创建并显示弹出窗口,然后在处理弹出窗口之前查询弹出窗口的 ViewModel 中的数据。它不是纯粹的 MVVM,因为您的 Client ViewModel 正在与 Popup 及其 ViewModel 进行一些相当直接的通信,但它会起作用,而且并不是那么大的罪过。
在这种无论如何都存在自然依赖性的情况下,我会采取务实的方法。 View 和 ViewModel 仍然是分离的。
我想这里有人可以指导一种更加解耦的方法——我也有兴趣阅读。
I'm no PRISM/MEF guru but if I were attacking this problem I'd take a slightly different approach and add a tad more decoupling. Essentially you want the ViewModels of your windows (Shell and Popup) to communicate - windows (Views) should only communicate with the user and update properties (in a decoupled, model-bound fashion) on their ViewModels.
Once you're in this position then Shell's ViewModel can request the user information (say from a property) of Popup's ViewModel. Though, of course, they are not the ViewModels of either Shell nor Popup - they are merely ViewModels that these Views happen to be bound to :)
Purists would go even further and talk about message queues between the various communicating parties but one step at a time, methinks.
Dan
Edit
Following Michael's comment:
As I said I'm not expert in PRISM but I think it depends how far you want to go with the decoupling. There's nothing stopping the Client ViewModel creating and showing the popup and then querying the popup's ViewModel for data before disposing of it. It's not pure MVVM because your Client ViewModel is doing some fairly direct communication with the Popup and its ViewModel but it will work and it's not that big a sin.
I would go with a pragmatic approach in cases such as this where a natural dependency exists anyway. You still have the separation of View and ViewModel.
I imagine there are folks here who can instruct on a more decoupled approach - which I would also be interested in reading about.