MVVM-light 中的清理与处置(布尔) [英] Cleanup vs Dispose(bool) in MVVM-light

查看:28
本文介绍了MVVM-light 中的清理与处置(布尔)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最新版本的 MVVM-light (V3 SP1) 中,ViewModel 类中的Dispose()"和Dispose(bool)"方法都被标记

In the latest version of MVVM-light (V3 SP1) both "Dispose()" and "Dispose(bool)" methods in ViewModel class are marked

不要再使用这种方法,它会在以后的版本中移除.改用 ICleanup.Cleanup()

Do not use this method anymore, it will be removed in a future version. Use ICleanup.Cleanup() instead

这是否意味着 IDisposable 接口不能在所有派生自 GalaSoft.MvvmLight.ViewModelBase 的 ViewModel 类中实现(并且必须覆盖清理)?

Does this mean that IDisposable interface must not be implemented in all ViewModel classes that are derived from GalaSoft.MvvmLight.ViewModelBase (and cleanup must be overrided)?

如果是的话, using 不能用于视图模型实例......可能我没有理解一些东西......请澄清......这种清理有什么好处?

If yes, using can't be used for view-model instances... Probably I didn't understand something... Please clarify... What are the benefits of such cleaning up?

谢谢.

推荐答案

这个问题是历史性的.起初我认为强制所有 VM 都为 IDisposable 是个好主意.然而,IDisposable 有一个不同的意图:一旦 VM 被处置,预计(按照惯例)它将尽快被垃圾收集.与朋友交谈后,我意识到强制所有 VM 都为 IDisposable 是一个错误.这就是我用 ICleanup 替换 IDisposable 的原因.ICleanup 的目的是提供一种清理 VM 的方法(例如将它们的状态刷新到持久存储、关闭流等),但不一定以尽快将它们垃圾收集的方式.

The issue is historical. At first I thought it would be a good idea to force all VMs to be IDisposable. However, IDisposable has a different intent: Once the VM is Disposed, it is expected (by convention) that it will be garbage collected as soon as possible. After talking to friends, I realize that forcing all VMs to be IDisposable was a mistake. This is why I replaced IDisposable by ICleanup. The intent of ICleanup is to provide a way to clean VMs (for example flushing their state to persistent storage, closing streams etc...) but not necessarily in a way that they will be garbage collected as soon as possible.

没有什么能阻止您让虚拟机实现 IDisposable.我只是不想在 ViewModelBase 类中保留这个约束,这就是为什么这个接口会在 V4 中被移除.

Nothing prevents you to make your VMs implement IDisposable. I just didn't want to keep this constraint in the ViewModelBase class, which is why this interface will be removed in V4.

使用 ICleanup 的好处是您可以在一次调用 ViewModelLocator.Cleanup() 中清理所有虚拟机.这是对 VM 开发人员的一个提示,即 VM 应该考虑为其 VM 提供一种清理方法.

The benefit of having ICleanup is that you can clean all your VMs in one call of ViewModelLocator.Cleanup(). It is a hint to VM developers saying that VMs should think of providing a cleanup method for their VMs.

有意义吗?干杯,洛朗

这篇关于MVVM-light 中的清理与处置(布尔)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆