MVVM轻工具包信使问题
我正在使用您的信使类在视图/控件和视图模型之间进行通信。 目前,我在同一视图上多次使用相同的用户控件。 问题是,当一个视图模型将消息发送回用户控件时,所有 在我看来,用户控件(相同类型)已更新。 如何防止这种情况发生?
仅供参考,我尝试了您提供的令牌功能,但我无法使其适用于我的特定情况。
I'm using your messenger class to communicate between views/controls and viewmodels.
Currently I have the same usercontrol multiple times on the same view.
The problem is that when one viewmodel sends a message back to the usercontrol then all of the
usercontrols (of that same type) in my view get updated.
How can this be prevented?
FYI, I played around with the token functionality that you provide, but I couldn't make it work for my particular case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过更改用户控件,以便在构造时可以指定它是否应该注册该特定消息?我会以此作为起点。
使用 Messenger.Default 时需要注意的另一件事 - 它不是线程安全的,因此如果您最终在用户控件以外的许多地方使用它(例如在工作线程中通知主线程发生的事件),那么您就最好将其包装在另一个执行必要锁定的类中。
Have you tried altering your usercontrol so that upon construction, you can specify whether or not it should Register for that particular message? I would do that as a starting point.
Another thing to watch out with using Messenger.Default -- it isn't threadsafe, so if you end up using it in lots of places other than usercontrols (like in worker threads to notify the main thread of events happening), then you had better wrap it in another class that performs the requisite locking.
如果您的消息继承自
MessageBase
类,您可以设置消息的目标,或使用
Guid
作为 Messenger 令牌。You could set the target of the message if your message inherits from
MessageBase
class,or use a
Guid
as a Messenger token.