我在我的第一个 WPF4 项目中使用 MvvmLight 4.0/C#,并且仍在学习 MVVM/WPF 绳索,我发现我以前的许多 Windows 窗体技能都无效。
无论如何,我有派生自 viewmodelbase
的 View 模型那Register
对于消息,同样让 View 为 VM/VM 和 VM/V 通信做同样的事情(注册消息)。所有关于清理的 mvvmlight 资源都说我应该 Unregister
消息以避免内存泄漏。
所以当我使用完 View 后,我只需调用 Messenger.Default.Unregister(this)
在 View /窗口的卸载事件中。当我用完 viewmodel
, 我只是调用 viewmodelbase.Cleanup()
在我的 viewmodel
引用假设基本实现将执行(一揽子)注销。
我想知道是否只是调用 Cleanup()
在 viewmodel
上足够了还是我必须在我的每个 View 模型中重写此方法并显式调用 Unregister
从每个覆盖内。现在,我在临时基础上创建/处理我的大部分 View 模型(不使用 SimpleIOC/ServiceLocator),并且只对取消注册清理中的所有消息感兴趣。
我发现以下 SO 标签已连接,但仍然没有回答我对简单调用 ViewModelBase.Cleanup()
的影响的查询。 vs 通过显式覆盖派生的方法取消注册 viewmodel
.
最佳答案
看看the code,毕竟它是开源的。
/// <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);
}
那么 Cleanup
方法实际上做了什么就很清楚了。如果您需要其他清理:
To cleanup additional resources, override this method, clean up and then call base.Cleanup()
关于c# - 在 MvvmLight View 模型上调用 Cleanup() 是否取消注册所有消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20614622/