PropertyChangedEventManager 何时创建以及何时附加?
长话短说……这是在一个大型 WPF 项目中,
我有一个实现 INotifyPropertyChanged
的“Patient”类。当我处置此类时,我会检查 PropertyChangedEventHandler
是否为空,如果不是,则将其运行到记录侦听器的 ListenerDetector
类,以便我们可以跟踪它们并清除泄漏。 Patient 确实有一些绑定到 WPF 元素的属性以及使用其 PropertyChanged
来监视更改的其他对象。
处理完其他所有内容后,我的日志结果如下:
日志:患者仍附加以下侦听器: - System.ComponentModel.PropertyChangedEventManager
问题: PropertyChangedEventManager
何时创建?
- 当创建 Patient 时
- 当 WPF 绑定到 Patient 属性时
- 其他一些点。
如果 Patient.PropertyChangedEventHandler
设置为 null
并因此与 PropertyChangedEventManager
断开连接,是否有任何方法可以重新创建 Manager 并让它监听 再次代码>Patient.PropertyChangedEventHandler
?请不要问为什么会发生这种情况,这是这里的一个紧张点:-(
To make a very long story short… This is in a large WPF project
I have a class “Patient” that implements INotifyPropertyChanged
. When I dispose this class I am checking that the PropertyChangedEventHandler
is null and if not run it to a ListenerDetector
class that logs the listeners so we can track them down and clean the leaks. Patient does have some properties that are bound to WPF elements as well as other objects the use its PropertyChanged
to monitor changes.
After disposing everything else, my log result is as follows:
Log: Patient still has the following listener(s) attached:
-System.ComponentModel.PropertyChangedEventManager
Question:
When is the PropertyChangedEventManager
created?
- When Patient is created
- When WPF binds to Patient property
- Some other point.
If Patient.PropertyChangedEventHandler
is set to null
and therefore disconnected from the PropertyChangedEventManager
is there any way to recreate the Manager and have it listen to the Patient.PropertyChangedEventHandler
again? Please don’t ask why this would happen, that is a point of tension here :-(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PropertyChangedEventManager
由 WPF 创建,以便支持绑定到任何实现INotifyPropertyChanged
的类。一旦您绑定到实现INotifyPropertyChanged
的任何类,它就会被创建并使用。话虽这么说,它是 WeakEventManager 模式的实现。虽然它仍然显示附加的侦听器,但请注意该侦听器是使用弱事件模式附加的。一旦发生完整的垃圾回收,这种情况就会消失,因为它使用弱引用来保存订阅。因此,此特定订阅不应成为(长期)内存泄漏的原因。
The
PropertyChangedEventManager
is created by WPF in order to support binding to any class that implementsINotifyPropertyChanged
. It'll be created and used as soon as you bind to any class that implementsINotifyPropertyChanged
.That being said, its an implementation of the WeakEventManager pattern. While it's still showing a listener attached, realize that this listener is attached using the Weak Event Pattern. This will go away as soon as a full garbage collection occurs, as it's using weak references to hold the subscription. As such, this particular subscription shouldn't be the cause of a (long term) memory leak.