现有绑定会在 Silverlight UIElements 生命周期的哪个阶段被删除?

发布于 2024-10-04 12:40:14 字数 245 浏览 6 评论 0原文

我正在尝试找出为什么我的一些 Silverlight 控件保留在内存中。我注意到,当我离开带有控件的页面时,仍然存在指向视图模型的引用链接。这些链接是在页面和视图模型之间创建绑定时创建的 ErrorsChanged 事件(我的视图模型实现 INotifyDataError)的剩余订阅的结果。在某些时候,一些但不是全部的绑定被删除,我无法弄清楚为什么这种情况没有发生。

在 Silverlight UIElements 生命周期的哪个阶段,现有的绑定会被删除?

I'm trying to work out why some of my Silverlight controls are staying in memory. I've noticed that when I navigate away from a page with the controls on, there remain referential links to the view model. These links are a result of left-over subscriptions to the ErrorsChanged event (my view model implements INotifyDataError) created when Bindings are created between the Page and the view model. At some point some but not all of the Bindings are removed and I can't work out why this isn't happening.

At what point in the life-cycle of Silverlight UIElements do existing Bindings get removed?

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

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

发布评论

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

评论(1

疯了 2024-10-11 12:40:14

我想我现在知道这个问题的答案了。如果我错了,请纠正我。绑定不会被删除。相反,作为绑定的特定实例化的结果,建立参考链接(例如连接 INotifyDataError 事件)。最简单的情况就是 DataContext 发生更改。

假设您在加载页面时将其 DataContext 设置为一个新的、不同的 INotifyDataError 对象。如果页面上有绑定(到 DataContext),则在 DataContext 对象被销毁之前,页面不会成为垃圾回收的候选者。这是因为 DataContext 通过其 ErrorsChanged 事件保存对 Page 的引用。如果您希望收集 DataContext 对象,则必须在页面的 Unloaded 事件中将 DataContext 设置为 null。

据我了解,实现 DataContext (与页面不同)的正确模式似乎是在页面上的 Loaded 事件中设置 DataContext,然后在页面触发其 Unloaded 事件时将 DataContext 设置为 null 。这个概念适用于任何框架元素。

顺便说一句,我还没有深入研究元素绑定。例如,当页面上的一个元素绑定到另一元素上的属性时。我不确定这些对象之间的引用链接何时被删除。我猜想是在页面卸载时。有人知道这个问题的答案吗?

I think I now know the answer to this. Please correct me if I'm wrong. Bindings are not removed. Instead referential links are made (e.g. INotifyDataError events are wired up) as a result of a particular instantiation of a Binding. In the simplest case that's when the DataContext changes.

Let's say you set the DataContext of a Page to a new, different INotifyDataError object as you load it. If there are Bindings on your Page (to the DataContext) the Page is not a candidate for garbage collection until the DataContext object is destroyed. That's because the DataContext holds a reference to the Page through its ErrorsChanged event. If you want the DataContext object to be collected you'll have to set the DataContext to null in the Page's Unloaded event.

As far as I understand it, it seems the proper pattern for implementing a DataContext (that is different from the Page) is setting the DataContext in the Loaded event on the page, and then setting the DataContext to null as the page fires its Unloaded event. This concept applies any Framework element.

As an aside, I haven't delved into element Bindings. For instance, when one element on a Page is bound to a property on another element. I'm not sure when the referential links between these objects are removed. I'm presuming it's when the page is unloaded. Anyone know the answer to this?

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