wpf 在 MvvmLight 视图模型上调用 Cleanup() 是否会取消注册所有消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20614622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does invoking Cleanup() on MvvmLight viewmodel unregisters all messages
提问by man.kar
I am using MvvmLight 4.0/C# in my first WPF4 project and still learning MVVM/WPF ropes where I find many of my former windows forms skills ineffective.
我在我的第一个 WPF4 项目中使用 MvvmLight 4.0/C# 并且仍在学习 MVVM/WPF 绳索,我发现我以前的许多 windows 窗体技能无效。
Anyway, I have viewmodels that derive from viewmodelbasethat Registerfor messages and likewise have views do the same (Register for messages) for VM/VM and VM/V communication. All mvvmlight resources on cleanup say that I should Unregistermessages to avoid memory leaks.
无论如何,我有从获得的ViewModelsviewmodelbase该Register消息和同样享有这样做(注册信息)的VM / VM和VM / V通讯。清理中的所有 mvvmlight 资源都说我应该Unregister发送消息以避免内存泄漏。
So when I am done using the views, I just call Messenger.Default.Unregister(this)in the unload event of the view/window. And when I am done using a viewmodel, I just invoke viewmodelbase.Cleanup()on my viewmodelreference assuming that the base implementation would do the (blanket) unregistering.
所以当我使用完视图时,我只调用Messenger.Default.Unregister(this)视图/窗口的卸载事件。当我使用完a 时viewmodel,我只是调用viewmodelbase.Cleanup()我的viewmodel参考,假设基本实现会执行(全面)取消注册。
I want to know if just invoking Cleanup()on the viewmodelis enough or do I have to override this method in each of my viewmodels and explicityly call Unregisterfrom within each override. For now I create/dispose most of my viewmodels on adhoc basis (not using SimpleIOC/ServiceLocator) and am only interested in unregistering all messages in the cleanup.
我想知道是否仅调用Cleanup()就viewmodel足够了,或者我是否必须在我的每个视图模型中覆盖此方法并Unregister从每个覆盖中显式调用。现在,我在临时基础上创建/处理我的大部分视图模型(不使用 SimpleIOC/ServiceLocator),并且只对在清理中取消注册所有消息感兴趣。
I found following SO tags connected but still leaves me unanswered on my query over implications of simply invoking ViewModelBase.Cleanup()vs Unregistering by explicitly overriding the method in the derived viewmodel.
我发现以下 SO 标记已连接,但仍然没有回答我的查询,即ViewModelBase.Cleanup()通过显式覆盖派生viewmodel.
Unregister(this) unregisters this instance from everything?
Unregister(this) 从所有东西中取消注册这个实例?
回答by ken2k
Just look at the code,it's open source after all.
看看代码吧,毕竟是开源的。
/// <summary>
/// Unregisters this instance from the Messenger class.
/// <para>To cleanup additional resources, override this method, clean
/// up and then call base.Cleanup().</para>
/// </summary>
public virtual void Cleanup()
{
MessengerInstance.Unregister(this);
}
So what the Cleanupmethod actually does it pretty clear. And in case you want other cleanups:
那么该Cleanup方法实际上做了什么就很清楚了。如果您想要其他清理工作:
To cleanup additional resources, override this method, clean up and then call base.Cleanup()
要清理其他资源,请覆盖此方法,清理然后调用 base.Cleanup()
回答by J King
No, cleanup will not auto unregister. You will have to override the method in each viewmodel and call Messenger.Default.Unregister(this). This will unregister the viewmodel from all events it has been registered for.
不,清理不会自动注销。您将必须覆盖每个视图模型中的方法并调用 Messenger.Default.Unregister(this)。这将从它已注册的所有事件中取消注册视图模型。
I was wrong. thanks ken2k. I found some other posts online that suggested it didn't. Can't seem to find them now. Anyway it looks like cleanup calls unregister for the viewmodel messages.
我错了。谢谢ken2k。我在网上发现了一些其他帖子,表明它没有。现在好像找不到了。无论如何,看起来清理调用取消注册视图模型消息。
THis is from LB himself:
这是 LB 本人的:
The intent of ICleanup is to provide a way to clean VMs (for example flushing their state to persistent storage, closing streams etc...)
ICleanup 的目的是提供一种清理 VM 的方法(例如将它们的状态刷新到持久存储、关闭流等...)
from This post on SO

