PRISM/MEF:2 个窗口如何才能实现?视图模型互相交谈?

发布于 2024-10-29 10:35:41 字数 439 浏览 1 评论 0原文

基本上,我不知道从哪里开始:

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云醉月微眠 2024-11-05 10:35:41

查看 Prism 中的事件聚合器。 Prism 中的聚合事件旨在作为促进解耦、视图模型间通信的一种手段。如果您想要“纯”MVVM,我认为它会是这样的:

  • 您的 ViewModel 发布一条消息(交互请求),它希望显示一个弹出窗口。
  • 您的视图正在侦听消息,并显示弹出窗口(将您的视图模型与理解如何显示提示分离)
  • 您的视图模型获取弹出窗口的结果(您的弹出窗口只是一个视图,并且不应该知道如何引发聚合事件) )
  • 您的 ViewModel 引发 Prism 聚合事件(包含用户输入的对象是有效负载)
  • 您的 shell 正在侦听该事件。

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:

  • Your ViewModel publishes a message (interaction request) that it want's to display a popup.
  • Your View is listening for the message, and shows the popup window (decouples your viewmodel from understanding how prompts are displayed)
  • Your ViewModel gets the results of the popup window (your popup window is just a view, and should know nothing about raising aggregate events)
  • Your ViewModel raises a Prism Aggregate Event (an object containing the user input is the payload)
  • Your shell is listening for that event.
梦醒灬来后我 2024-11-05 10:35:41

我不是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文