在Messenger类中使用令牌的正确方法是什么?

编程入门 行业动态 更新时间:2024-10-11 07:34:16
本文介绍了在Messenger类中使用令牌的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用MVVM Light Toolkit的3.0.3.19版本.

I am using version 3.0.3.19 of the MVVM Light Toolkit.

来自 blog.galasoft.ch/archive/2010/03/16/whatrsquos-new-in-mvvm-light-v3.aspx :

现在可以使用令牌通过Messenger发送邮件.

Messages can now be sent through the Messenger with a token.

  • 要发送带有令牌的消息,请使用重载Send(TMessage消息,对象令牌)方法.

  • To send a message with token, use the method overload Send(TMessage message, object token).

要接收带有令牌的消息,请使用方法Register(对象收件人,对象令牌,Action操作)或Register(对象收件人, 对象令牌,bool receiveDerivedMessagesToo,操作操作)

To receive a message with token, use the methods Register(object recipient, object token, Action action) or Register(object recipient, object token, bool receiveDerivedMessagesToo, Action action)

令牌可以是简单值(int,字符串等)或实例 一堂课该邮件不会传递给已注册的收件人 带有其他令牌或完全没有令牌.

The token can be a simple value (int, string, etc…) or an instance of a class. The message is not delivered to recipients who registered with a different token, or with no token at all.

根据上述文档,我已经在ViewModel A中尝试了以下方法:

According to the documentation above, I have tried the following in ViewModel A:

Messenger.Default.Send(new NotificationMessage("message"), "token");

在ViewModel B中附带以下内容:

Along with the following in ViewModel B:

Messenger.Default.Register<NotificationMessage>(this, "token", (msg) => Console.WriteLine(msg.Notification));

但是,永远不会执行该回调.我在做什么错了?

However, the callback is never executed. What am I doing wrong?

推荐答案

我的ViewModelLocator在ViewModel B之前初始化ViewModelA.换句话说,该消息已由ViewModel A正确发送,但是ViewModel B尚未真正发送收到.

My ViewModelLocator was initializing ViewModel A before ViewModel B. In other words, the message was being sent properly by ViewModel A, but ViewModel B was not yet around to actually receive it.

我在ViewModelLocator中更改了初始化顺序,问题得以解决.此外,请验证Messenger是否可以使用String以外的其他类型的令牌.

I changed the order of initialization in the ViewModelLocator and the problem was solved. Also, verified that the Messenger worked with tokens of other types besides String.

更多推荐

在Messenger类中使用令牌的正确方法是什么?

本文发布于:2023-11-27 22:39:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639891.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:令牌   类中   正确   方法   Messenger

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!