问题
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;
}
}
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)
Edit:
To make my problem clearer: I know that I can use the Register method:
Messenger.Default.Register<List<string>>(this, StringList);
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)
回答1:
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.
see these:
http://blog.galasoft.ch/lbugnion/archive/2009/09/27/mvvm-light-toolkit-messenger-v2-beta.aspx
MVVM light: Pass object from view to viewmodel
来源:https://stackoverflow.com/questions/16593328/mvvm-light-register-generic-class-for-messenger