注册在MVVM光一个视图模型之外的信息? [英] Registering for messages outside of a ViewModel in MVVM Light?
问题描述
我想注册一个消息的视图模型之外的静态构造函数,但显然该注册并没有采取:已注册的动作绝然,当邮件被发送。我试图传递null或为收件人一个新的对象
参数登记时,但没有奏效。
I was trying to register for a message outside of a ViewModel in a static constructor but apparently that registration didn't take: the registered action never ran when messages were sent. I tried passing in null or a new object for the recipient
parameter when registering but that didn't work.
我有一种感觉,指定收件人必须是重要的莫名其妙,但我不知道为什么。我认为,所有收件人都应该接受广播消息呢。有没有一种方法,使这项工作,或者这根本不支持?
I have a feeling the specifying the recipient must be important somehow, but I don't know why. I thought that all recipients were supposed to get broadcasted messages anyway. Is there a way to make this work or is this simply not supported?
推荐答案
当您使用的收件人是很重要的 Messenger.Register
为 Messenger.Send
这并不重要。然而,在<打击>一个已知的bug 的WeakReference
击> WeakAction
在MVVM实现,持有引用到收件人,但是,它应该释放它。
The recipient is important when you use Messenger.Register
for Messenger.Send
it is not important. However, there is a known bug in the WeakReference
WeakAction
implementation in MVVM that holds a reference to the recipient, although, it should release it.
总之,如果你是内视图模型,请确保你调用清理
。在视图中,调用 Messenger.Unregister(本)
在卸载
事件,例如
In short, if you are inside a ViewModel, make sure that you call Cleanup
. In a view, call Messenger.Unregister(this)
in the Unloaded
event e.g.
public MyView() {
this.Unloaded += (o, e) => { Messenger.Unregister(this); }
}
在其他类你将不得不要么实施的IDisposable
或者使用一些其他的机制来注销该邮件收件人。
In other classes your will have to either implement IDisposable
or use some other mechanism to unregister the message recipient.
另请参阅:
- <一个href="http://stackoverflow.com/questions/6319862/mvvm-light-listener-not-releasing-deterministic-finalization-for-registered-obj/6324552#6324552">MVVM光监听器不释放/确定性终结了注册的对象?
- <一个href="http://stackoverflow.com/questions/6919209/does-mvvm-light-v3-unregister-work-properly/6922299#6922299">Does MVVM光V3注销正常工作?
- <一个href="http://stackoverflow.com/questions/7036873/when-to-dispose-viewmodel-in-mvvm-light/7037282#7037282">When处置视图模型中MVVM光
- MVVM Light Listener not releasing / deterministic finalization for registered object?
- Does mvvm light v3 unregister work properly?
- When to dispose ViewModel in MVVM Light
这篇关于注册在MVVM光一个视图模型之外的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!