MVVM-Light:带有 TTarget 的 Messenger
嗨,我尝试了解如何直接向特定目标发送消息。我有一个小型 SL 项目和一个名为 class1 的虚拟类。我在类构造函数中注册并监听所有字符串类型消息。
public class Class1
{
public Class1()
{
Messenger.Defaut.Register<String>(this, f => { TraiteMessage(f); });
}
public void TraiteMessage(string sMessage)
{
MessageBox.Show("Class1:" + sMessage);
}
}
现在我发送这样的消息:
Messenger.Defaut.Send
消息未到达目标。当我查看 MVVM light 的内部源代码时,我可以看到测试是这样完成的: item.Action.Target.GetType() == messageTargetType 但实际上 messageTargetType 设置为: MyProejet.Class1 但项目。 Action.Target.GetType() 返回类似以下内容: {System.Collections.Generic.Dictionary2System.Type,System.Collections.Generic.List
1[SLDuninLib.Messages.Avise+WeakActionAndToken]]} System .Type {System.RuntimeType}
我做错了什么? 感谢您的帮助
HI, I try to understant how to send a message directly to a speciciel Target. I got a small SL projet and and a dunmmy class named class1. I register in the class constructor and listenig for all string type message.
public class Class1
{
public Class1()
{
Messenger.Defaut.Register<String>(this, f => { TraiteMessage(f); });
}
public void TraiteMessage(string sMessage)
{
MessageBox.Show("Class1:" + sMessage);
}
}
Now I send the message like this:
Messenger.Defaut.Send<String, Class1>("test message");
The message do not reach the target. When I look into the internal source code of the MVVM light i can see that the test are done like: item.Action.Target.GetType() == messageTargetType but in fact the messageTargetType are set to: MyProejet.Class1 but the item.Action.Target.GetType() return something like: {System.Collections.Generic.Dictionary2System.Type,System.Collections.Generic.List
1[SLDuninLib.Messages.Avise+WeakActionAndToken]]} System.Type {System.RuntimeType}
what do i do wrong?
Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是否在任何地方创建了 Class1 的实例?如果不是,那么它永远不会注册接收任何消息。
您可以通过在 Class1 构造函数中注册接收消息的代码行上放置一个断点来轻松测试这一点。
我测试了您的代码,当我在另一个视图模型中注册消息时它可以工作,但这是因为 ViewModelLocator 中的任何视图模型都是在加载 App.xaml 后立即创建的。
Is an instance of Class1 created anywhere? If not, then it never registers to receive any messages.
You can easily test this by putting a breakpoint on the line of code that registers to receive the message in the Class1 constructor.
I tested your code and it works when I have the message registration in another view model, but this is because any view models in the ViewModelLocator are created as soon as the App.xaml is loaded.
当定义 TTarget 时,消息通过其类型而不是实例来识别。
因此,您的代码不起作用(正如您所经历的),因为您发送了 Class1 类型的消息,但您注册的收件人是 String 类型,而不是 Class1 类型。
所以,在这种情况下,可以考虑多种解决方案。
Messenger.Defaut.Register(this, msg => { Class1.TraiteMessage(msg); });
Messenger.Defaut.Send("test message");
公共结构SearchTerm
{
公共字符串类别;
公共字符串关键字;
}
发送如下:
Messenger.Default.Send(new SearchTerm{Category = "Q",Keyword = "something"}));
并在 Class1 中使用,如下所示:
Messenger.Default.Register(this,msg => {TraiteMessage(msg.Keyword);});
对于我来说,最后一个解决方案是首选。
干杯
When TTarget defined, the message is awared by its type, not instance.
So, your code did not work (as you experienced) because you sent a message with Class1 type but receipient you register was String type, not Class1 type.
So, in this case, several solutions can be considered.
Messenger.Defaut.Register<Class1>(this, msg => { Class1.TraiteMessage(msg); });
Messenger.Defaut.Send<String, String>("test message");
public struct SearchTerm
{
public string Category;
public string Keyword;
}
send as below:
Messenger.Default.Send(new SearchTerm{Category = "Q",Keyword = "something"}));
and consume in Class1 as below:
Messenger.Default.Register<SearchTerm>(this,msg => {TraiteMessage(msg.Keyword);});
For me, the last solution is prefered.
Cheers