我开始学习适用于 Windows Phone 8 的 LINQ-to-SQL
,遇到了 this MSDN 上的文章。
它们显示了 DataContext
的基类,它实现了 INotifyPropertyChanging
和 INotifyPropertyChanged
。 INotifyPropertyChanging
的原因是:
◦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/