MVVM Light Listener是否不释放/确定对象的确定性终结? [英] MVVM Light Listener not releasing / deterministic finalization for registered object?

查看:133
本文介绍了MVVM Light Listener是否不释放/确定对象的确定性终结?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有关联虚拟机的子窗口,每次我要求打开子窗口时都会创建该子窗口.当子窗口打开时,它将为MVVM Light消息注册一个侦听器.关闭窗口后,我很确定要释放对它的所有引用,但实际上没有调用dispose,因为它没有实现IDisposeable.

I have a childwindow with an associated VM that gets created each time I ask the child window to open. when the childwindow opens, it registers a listener for an MVVM Light message. After I close the window, I'm pretty sure that I'm releasing all references to it, but I don't actually call dispose because it does not implement IDisposeable.

当我实例化另一个相同类型的子窗口并将其发送给不同的上下文时,我知道我收到的是来自先前的VM实例化的消息……每次我使用该窗口时,越来越多VM正在侦听,并且代码重复.

When I instanciate another child window of the same type, and send it a different context, I know that I'm receiving the message from the previous instanciation of the VM... each time I use the window, more and more VM are listening, and the code repeats.

如何确定我先前注册用来收听消息的VM实际上已经释放并且不再处于活动状态.有确定的方法可以做到这一点吗?

How can I be sure that my previous VM that registered to listen to a message, has actually been released and is no longer active. Is there a deterministic way to do this?

推荐答案

每当您注册一条消息时,都应确保也取消注册该消息.要注销,可以对ViewModelBase派生的类使用Cleanup方法.在其他情况下,例如一个视图,您应该实现一个在卸载视图时调用方法hat的方法-例如,通过捕获和处理控件或视图上的unload事件.然后,使用此方法调用Messenger.Unregister(EventTarget).

Whenever you register a message you should make sure that you unregister the message as well. To unregister you can use Cleanup method on classes deriving from ViewModelBase. In other cases, e.g. a view, you should implement a method hat is called when the view is unloaded - e.g by trapping and handling the unloaded event on a control or view. In this method you then call Messenger.Unregister(EventTarget).

此行为是该工具箱当前版本中的一个古怪现象,洛朗(Laurent)意识到这一点.

This behaviour is a quirk in the current version of the toolkit, and Laurent is aware of it.

这篇关于MVVM Light Listener是否不释放/确定对象的确定性终结?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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