清理VS的Dispose(布尔)在MVVM光 [英] Cleanup vs Dispose(bool) in MVVM-light

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

问题描述

在最新版本的MVVM光的(V3 SP1)既是的Dispose()和处置(布尔),在视图模型类的方法被标记为

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派生的所有视图模型类实现(和清理必须overrided)?

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)?

如果有,使用,不能用于视图模型实例......也许我不明白的东西......请澄清...什么是的优点,例如清理?

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?

感谢。

推荐答案

这个问题是历史原因。起初,我认为这将是一个好主意,强制所有的虚拟机是IDisposable接口。然而,IDisposable的具有不同的意图:一旦该VM被布置,预期(约定),这将是垃圾尽快收集。谈朋友后,我认识到,强迫所有的虚拟机是IDisposable的是一个错误。这就是为什么我换成了IDisposable由ICleanup。 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.

没有prevents,使您的虚拟机实现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之一()调用。这是一个提示,虚拟机开发商说,虚拟机应该考虑为他们的虚拟机提供的清理方法。

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.

这是否有意义吗? 干杯, 洛朗

Does that make sense? Cheers, Laurent

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

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