MVVM Light:注册Messenger的通用类 [英] MVVM Light: Register generic class for messenger

查看:72
本文介绍了MVVM Light:注册Messenger的通用类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用的ShowMessage类,我想用它来携带数据:

I have a generic ShowMessage class that I want to use to carry data around:

class ShowMessage<T> {
    private readonly T _Data;

    public string Title { get; set; }
    public T Data { get { return _Data; } }

    public ShowMessage( T data) {
        _Data = data;
    }
}

在我的ViewModel中,我想注册所有可能的ShowMessage实例,但是目前我还没有找到方法.有没有人注册过通用基类(请注意:我只想注册一次,而不要n次注册干净的代码)

In my ViewModel I want to register for all possible ShowMessage instances, but currently I haven't found a way. Has anyone registered a generic base class (note: I want to register only once and not n-times to have a clean code)

为了使我的问题更清楚:我知道我可以使用Register方法:

To make my problem clearer: I know that I can use the Register method:

Messenger.Default.Register<List<string>>(this, StringList);

在这种情况下,我注册了List<string>.我想注册List<>,让一个处理程序作用于不同的有效负载(因为该处理程序不需要了解任何有关有效负载的信息)

In this case I register for List<string>. I want to register for List<> to have one handler acting on different payload (as the handler is not required to know anything about the payload)

推荐答案

mvvm light Messenger允许您注册带有特定对象的消息,只需将该注册放置在全局可用的部分代码中即可,但是您可以像在父视图模型中那样进行操作,然后使用ShowMessage类型注册消息.然后,只要任何类发送了该消息,只要附加了ShowMessage类,该消息就会被捕获.

the mvvm light messenger allows you to register for a message with a specific object attached, just place that registration in a part of your code that is globally available, however you do that, like in the parent view model, then you register for messages with type ShowMessage. Then any time that message is sent by any class, it will be caught as long as the ShowMessage class is attached.

请参阅以下内容:

http ://blog.galasoft.ch/lbugnion/archive/2009/09/27/mvvm-light-toolkit-messenger-v2-beta.aspx

MVVM灯:将对象从视图传递到视图模型

这篇关于MVVM Light:注册Messenger的通用类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆