MVVM Mediator 多个实例

发布于 2024-08-09 07:54:04 字数 323 浏览 12 评论 0原文

有人可以解释一下中介模式如何与多个实例一起工作吗?

我在视图中的代码:

public MyView() {
    Mediator.Register("CloseWindow",()=>Close());
}

和在 ViewModel 中:

public SomeMethod() {
    Mediator.Notify("CloseWindow");
}

只要 View - ViewModel 对只有一个实例,就可以找到此代码。

多个实例如何解决?

Can someone explain how the mediator pattern works with multiple instances.

My code in the view:

public MyView() {
    Mediator.Register("CloseWindow",()=>Close());
}

and in the ViewModel:

public SomeMethod() {
    Mediator.Notify("CloseWindow");
}

This works find as long as there is only one instance of the View - ViewModel pair.

How do I solve it with multiple instances?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

梦里兽 2024-08-16 07:54:05

我不知道你的中介模式的特定实现是如何工作的,但在我的模式中你可以发送更多的信息而不仅仅是字符串。

例如:

public MyView() {
    Mediator.Register<CloseWindowMessage>(message =>
    {
        if (message.ViewModel == DataContext) Close();
    });
}    

在 ViewModel 中:

public SomeMethod() {
    Mediator.Notify(new CloseWindowMessage(this));
}

在此示例中,ViewModel 将其自身作为参数传递给视图。然后视图可以检查消息是否是从其视图模型发送的。

I don't know how your particular implementation of the mediator pattern works, but in mine you can send more information than just strings.

For example:

public MyView() {
    Mediator.Register<CloseWindowMessage>(message =>
    {
        if (message.ViewModel == DataContext) Close();
    });
}    

and in the ViewModel:

public SomeMethod() {
    Mediator.Notify(new CloseWindowMessage(this));
}

In this example, the ViewModel passes itself as a parameter to the view. The view can then check that the message is being sent from its view model.

昔日梦未散 2024-08-16 07:54:04

我使用替代解决方案。 MyView 实现了一个包含 Close 方法的接口 IMyView。 MyViewModel 对象关联 View,因此它可以通过接口调用 Close 方法。

如果您对具体示例感兴趣,则可以查看:

WPF 应用程序框架 (WAF)

I use an alternative solution. MyView implements an interface IMyView which contains the Close method. The MyViewModel object associates the View and so it can call the Close method through the interface.

If you are interested in a concrete example then you might have a look at:

WPF Application Framework (WAF)

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