mvvmlight Messenger的奇怪行为 [英] mvvmlight messenger strange behaviour

查看:275
本文介绍了mvvmlight Messenger的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有一个奇怪的行为.我使用MvvmLight Messenger通知用户界面的不同部分进行更新.

I have a strange behavior in my project. I use MvvmLight messenger to notify different parts of my UI to update.

   public EntryViewModel(MenuViewModel menuVM, Entry item)
    {
        this._menuVM = menuVM;
        OpenDetailsCommand = new RelayCommand(OpenDetailsInMainWindow);
        BackCommand = new RelayCommand(Back);

        this._entry = item;
        Refresh(); 

        Messenger.Default.Register<CardUpdateMessage>(this, this._entry.Id, msg => Refresh(); );   
 }

并使用

Messenger.Default.Send(new CardUpdateMessage(id), id);

当我在注册后看到Messenger内容时,它包含CardUpdateMessage的约400个已注册操作,但是当我打电话时,它们均不会触发. 顺便说一句,每个Message类型只有一个注册对象的类似代码可以正常工作.是什么原因造成的?

when I see Messenger content after registration it contains about 400 registered actions of CardUpdateMessage, but when I call send none of them fires. By the way, similar code with single registered object per Message type work as I expect. What is the cause of this problem?

更新:我对调试器进行了一些研究,发现在文件

Update: I have made some research with debugger and found that in file https://mvvmlight.codeplex.com/SourceControl/latest#GalaSoft.MvvmLight/GalaSoft.MvvmLight (PCL)/Messaging/Messenger.cs method SendToList is fired and in its inner loop WeakAction is fired, but Refresh method is not run. Does it something I haven't suspected with WeakAction?

解决方案:我发现了此问题.由于意外行为,不允许在Messenger.Register中使用匿名功能. 只是Messenger.Default.Register(this, this._entry.Id, Refresh);并且 private void Refresh(CardUpdateMessage msg) ...

Solution: I have found the case of this issue. It's NOT ALLOWED to use anonimous function in Messenger.Register due to unexpected behavior. just Messenger.Default.Register(this, this._entry.Id, Refresh); and private void Refresh(CardUpdateMessage msg) ...

外部参考: https://mvvmlight.codeplex.com/workitem/7640

推荐答案

这是答案 https://mvvmlight.codeplex.com/workitem/7640 和这个 具有行为,局部变量和MVVM light Messenger中的垃圾回收

简而言之,解决方案-请勿在Messenger.Default.Register中使用lambda,因为它们的行为可能不符合您的预期.

Solution in short words - do no use lambdas in Messenger.Default.Register, because the can behave not as you expected.

这篇关于mvvmlight Messenger的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆