c# - 在 MvvmLight View 模型上调用 Cleanup() 是否取消注册所有消息

标签 c# wpf mvvm mvvm-light

我在我的第一个 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 .

Unregister(this) unregisters this instance from everything?

When and where to unregister messenger with mvvmlight

最佳答案

看看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/

相关文章:

wpf - MVVM 在执行附加到按钮的命令之前显示确认消息框

c# - MVVM 属性取决于对象图

c# - 通过单元格的值设置选定的行datagridview

javascript - 对于 Controller 的 Ajax Post 方法,参数作为空值传入

WPF - 自定义控件 + ICommand(如何实现)?

c# - 标记在 XML 命名空间中不存在

c# - 如何使用MVVM从Restful Service向Xamarin.Forms View 显示图像

c# - 带和不带 .aspx 扩展名的链接

c# - WP7中显示MessageBox时关闭默认声音

c# - WPF 浏览器调用脚本