是否可以通过 MVVM Light Messenger 仅发送令牌?
我一直在重构一些最初在 MVVM Foundation 中使用 Messenger
的代码,现在在 MVVM Light Toolit 中使用 Messenger
。我似乎无法找到等效的一件事是您想要做的就是发送一个令牌(即,令牌既充当消息的唯一标识符,又充当消息的唯一标识符和消息本身)。
原始代码(MVVM Foundation) - 一个字符串即可解决所有
// send code
mvvmFoundationMessenger.NotifyColleagues("QuestionTimedOut");
// register code
mvvmFoundationMessenger.Register(
"QuestionTimedOut",
() => UpdateOnQuestionTimedOut());
新代码(MVVM Light) - 还有比这更优雅的解决方案吗?
// send code
mvvmLightMessenger.Send("QuestionTimedOut", "QuestionTimedOut");
// register code
mvvmLightMessenger.Register<string>(
this,
"QuestionTimedOut",
token => UpdateOnQuestionTimedOut());
我意识到我可以显式地新建一个 NotificationMessage
但这会添加更多代码。
I've been refactoring some code that was originally using the Messenger
in MVVM Foundation to now use the Messenger
in MVVM Light Toolit. One thing that I can't seem to find an equivalent for is the case where all you want to do is send a Token (i.e, the Token is acting as both a unique identifier for the message and the message itself).
Original Code (MVVM Foundation) - one string does it all
// send code
mvvmFoundationMessenger.NotifyColleagues("QuestionTimedOut");
// register code
mvvmFoundationMessenger.Register(
"QuestionTimedOut",
() => UpdateOnQuestionTimedOut());
New Code (MVVM Light) - is there a more elegant solution than this?
// send code
mvvmLightMessenger.Send("QuestionTimedOut", "QuestionTimedOut");
// register code
mvvmLightMessenger.Register<string>(
this,
"QuestionTimedOut",
token => UpdateOnQuestionTimedOut());
I realize I could explicitly new up a NotificationMessage
but that would add even more code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好点。我把它放在 MVVM Light VNext 的积压工作中。
Good point. I put that on the backlog for MVVM Light VNext.
您可以从 MvvmLight 的
Messenger
派生并添加您自己的便捷方法,也可以添加Messenger
扩展类中的扩展方法。You could derive from MvvmLight's
Messenger
and add your own convenience methods or you could addMessenger
Extension Methods in an extension class.