gpt4 book ai didi

c# - mvvmlight 信使奇怪的行为

转载 作者:行者123 更新时间:2023-12-03 10:34:12 25 4
gpt4 key购买 nike

我的项目中有一个奇怪的行为。我使用 MvvmLight messenger 来通知我的 UI 的不同部分进行更新。

   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 内容时,它包含大约 400 个注册的 CardUpdateMessage 操作,但是当我调用 send 时,它们都没有触发。
顺便说一句,每个消息类型具有单个注册对象的类似代码可以按我的预期工作。这个问题的原因是什么?

更新 : 我用调试器做了一些研究,发现在文件 https://mvvmlight.codeplex.com/SourceControl/latest#GalaSoft.MvvmLight/GalaSoft.MvvmLight (PCL)/Messaging/Messenger.cs 方法 SendToList 被触发并在其内部循环中触发 WeakAction,但 Refresh 方法未运行。这是我没有怀疑过 WeakAction 的事情吗?

解决方案 : 我找到了这个问题的案例。由于意外行为,不允许在 Messenger.Register 中使用匿名函数。
只是 Messenger.Default.Register(this, this._entry.Id, Refresh);private void Refresh(CardUpdateMessage msg) ...
外部引用 : https://mvvmlight.codeplex.com/workitem/7640

最佳答案

这就是答案https://mvvmlight.codeplex.com/workitem/7640还有这个
Strange behavior with actions, local variables and garbage collection in MVVM light Messenger

简短的解决方案 - 不要在 Messenger.Default.Register 中使用 lambda,因为它的行为可能与您预期的不同。

关于c# - mvvmlight 信使奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39911423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com