2016-10-07 26 views
1

我在我的項目中有一個奇怪的行爲。我使用MvvmLight messenger來通知我的用戶界面的不同部分進行更新。mvvmlight信使奇怪的行爲

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); 

發送當我看到信使內容登記後,大約包含400個註冊的行爲CardUpdateMessage的,但是當我打電話發都不他們閃光。 順便說一句,每個消息類型的單個註冊對象的類似代碼按我期望的那樣工作。這個問題的原因是什麼?

更新:我已經取得了一些研究與調試,發現文件https://mvvmlight.codeplex.com/SourceControl/latest#GalaSoft.MvvmLight/GalaSoft.MvvmLight(PCL)/Messaging/Messenger.cs方法SendToList被激發,並在其內部循環WeakAction被解僱,但刷新方法不運行。這是否與WeakAction沒有懷疑?

解決方案:我發現這個問題的情況。由於意外行爲,不允許在Messenger.Register中使用匿名函數。 只是Messenger.Default.Register(this, this._entry.Id, Refresh);private void Refresh(CardUpdateMessage msg) ...

外置參考https://mvvmlight.codeplex.com/workitem/7640

+1

很高興您找到了解決方案,但不應該將其編輯到您的問題中。只需在下面添加一個答案與您發現的內容,並在所需的等待期過後,您可以選擇正確的答案並將其關閉。這看起來可能很奇怪,但這就是我們在這裏做的。 – Will

+0

@好的,我寫了我的答案 – Anton

回答