PropertyChangedEventManager 何时创建以及何时附加?

发布于 2024-12-12 01:41:57 字数 819 浏览 0 评论 0原文

长话短说……这是在一个大型 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薯片软お妹 2024-12-19 01:41:57

PropertyChangedEventManager 由 WPF 创建,以便支持绑定到任何实现 INotifyPropertyChanged 的类。一旦您绑定到实现 INotifyPropertyChanged任何类,它就会被创建并使用。

话虽这么说,它是 WeakEventManager 模式的实现。虽然它仍然显示附加的侦听器,但请注意该侦听器是使用弱事件模式附加的。一旦发生完整的垃圾回收,这种情况就会消失,因为它使用弱引用来保存订阅。因此,此特定订阅不应成为(长期)内存泄漏的原因。

The PropertyChangedEventManager is created by WPF in order to support binding to any class that implements INotifyPropertyChanged. It'll be created and used as soon as you bind to any class that implements INotifyPropertyChanged.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文