c# - INotifyPropertyChanging 接口(interface)如何帮助限制内存消耗

标签 c# .net windows-phone-8 linq-to-sql

我开始学习适用于 Windows Phone 8 的 LINQ-to-SQL,遇到了 this MSDN 上的文章。

它们显示了 DataContext 的基类,它实现了 INotifyPropertyChangingINotifyPropertyChangedINotifyPropertyChanging 的原因是:

◦The INotifyPropertyChanged interface is used for change tracking.

◦The INotifyPropertyChanging interface helps limit memory consumption related to change tracking.

这篇文章没有给出任何具体的引用来证明对 INotifyPropertyChanging 接口(interface)的内存消耗是合理的。 INotifyPropertyChanging上的文章本身只是说:

Notifies clients that a property value is changing.

有人可以向我解释一下这个接口(interface)如何限制应用程序的内存占用,只是通过通知属性值即将更改(甚至不限制该更改发生)吗?

最佳答案

我只能推测,但我认为这就是作者的想法:

在没有 INotifyPropertyChanging 的世界中,如果消费者需要属性的旧值,它必须抢先缓存它(因为,一旦引发 PropertyChanged 事件,为时已晚,值(value)已经改变)。或者,生产者可以在不同的属性中保留旧值的副本。无论哪种方式,数据始终保持重复。

有了 INotifyPropertyChanging,消费者不需要事先缓存任何东西。当引发 PropertyChanging 事件时,它可以获取旧值,知道它即将发生变化。然后引发 NotifyPropertyChanged 事件,消费者可以获取新值,对两者执行任何操作,然后删除它们。数据仍然是重复的,但只是在特定的时间点和有限的持续时间内。

关于c# - INotifyPropertyChanging 接口(interface)如何帮助限制内存消耗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25627218/

相关文章:

c# - 使用 POST 请求向 Jira API 发送 JSON 时出现 System.Net.WebException

windows-phone-8 - 如何在 Windows Phone 8 上获取文件夹中的文件?

html - 在 Windows Phone 8 的 cordova 应用程序中禁用用户缩放

c# - 单独项目中的 EF5 模型看不到 DbContext 方法

c# - 使用下载服务将 IsRefreshing 绑定(bind)到 IsBusy

c# - 如何在代码中向 WPF 窗口添加形状?

c# - CSV 中双引号值的正则表达式

c# - 我们如何在设置和部署项目中设置产品名称、公司名称等?

c# - SMTP 服务器需要安全连接 - gmail

c# - ASP.NET 表单值验证场景