WPF 中 CollectionViewSource.GetDefaultView() 内存泄漏?

发布于 2024-09-25 10:48:48 字数 310 浏览 9 评论 0原文

我想知道在 WPF 中使用 CollectionView 的最佳实践是什么,特别是使用 CollectionViewSource.GetDefaultView() 方法。有人在使用 GetDefaultView 时遇到过任何问题吗?我们担心它可能会导致一些内存泄漏。

手动创建 ListCollectionViews 通常是比依赖 GetDefaultView() 更好的做法吗?

WPF 如何管理这些视图?当集合被GC时,它们会被GC吗?我看过一些文章说您需要将 CollectionViews 从其源集合中分离出来,否则会出现内存泄漏。

谢谢你的帮助!

I'm wondering what the best practices are for working with CollectionViews in WPF in particular the use of the CollectionViewSource.GetDefaultView() method. Has anyone run into any problems with using GetDefaultView? We're concerned that it may be causing some memory leaks.

Is it generally a better practice to create ListCollectionViews manually rather than rely on GetDefaultView()?

How does WPF manage these views? Do they get GC'd when the collection does? I've seen some articles saying that you need to detach CollectionViews from their source collection otherwise you'll have a memory leak.

Thanks for you help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

我很OK 2024-10-02 10:48:48

Bea 表示,使用 CollectionViewSource.GetDefaultView() 是她最喜欢的访问方式给定集合的视图。此外,我个人在使用 CollectionViewSource.GetDefaultView() 时从未遇到过任何问题,但在给定场景中这当然是可能的。

您必须了解每个集合都有一个默认视图,并且当您将给定属性绑定到给定集合时,WPF 会自动用视图包装集合并绑定到视图;不是集合。

由于这是 WPF 框架的一部分,因此您必须假设每个集合都有一个与之关联的视图,并且当集合被处置时,该视图也会被处置。如果通过可绑定属性对集合进行引用,则无论您是否调用 CollectionViewSource.GetDefaultView() ,都会有一个引用该集合的关联视图,因此您仍然会处于相同的困境。

经验证据< /a> 存在,直到集合超出范围为止,关联的视图不会被 GC 回收;但请记住,这是处理带有实现 INotifyCollectionChanged 的集合。

根据您的情况,集合和关联视图之间可能存在 1:1 关系,因此耦合可能不会提供任何障碍。在这种情况下,一旦集合超出范围,视图被 GC 处理就不是问题了。

归根结底,就是要谨慎行事,并确保您了解更复杂情况下发生的情况,以便采取适当的措施。

Bea states that using CollectionViewSource.GetDefaultView() is her favorite way for accessing the view of a given collection. In addition I have personally never ran into any issues making use of the CollectionViewSource.GetDefaultView() however it is certainly possible in a given scenario.

You have to understand that every collection has a default view and that when you bind a given property to a given collection WPF automatically wraps the collection with a view and binds to the view; not the collection.

Since this is part of the WPF framework you then have to assume that with every collection having a view associated with it that it would then be disposed of when the collection is disposed of. If there is a reference to the collection via a bindable property there is then an associated view referencing the collection whether you make a call to CollectionViewSource.GetDefaultView() or not so you would still be in the same predicament.

Empirical evidence exists that until the collection is out of scope the associated views will not be GC'ed; however keep in mind that this was dealing with a collection with implements INotifyCollectionChanged.

Based on your circumstance a 1:1 relationship between the collection and associated view may exist and therefore the coupling may provide no hindrance whatsoever. In this scenario the view being GC'ed once the collection has gone out of scope is not an issue.

It boils down to treading with caution and making certain that you know what is happening under the covers for the more complex situations so that you can take the appropriate measures.

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