“尝试”的事件财产变化
(使用.Net 3.5)我有一个从外部数据源导入设置的例程,之后我需要向用户显示一个伪日志,详细说明哪些属性已更新。通常我会通过 property_changed 事件跟踪属性更新。但是,当数据源中的值恰好已经等于属性值时,即使调用了属性集,也永远不会引发 property_changed 事件。但我仍然需要向用户显示从数据源“更新”的属性。
这里的常见模式是什么?我的属性设置器中是否需要另一个事件(除了 property_changed 之外)-“property_attemptedset”?
编辑:所以一个建议是我根本不在属性设置器中进行相等检查,这样“property_changed”无论如何都会被解雇。但我总是被告知要过滤实际值的变化。这不是最佳实践吗?
(using .Net 3.5) I have a routine that imports settings from an external datasource, after which I need to display a pseudo-log to the user detailing which properties have been updated. Usually I would track property updates through the property_changed events. However, when the value from the datasource happens to already equal the property value, then property_changed event never gets raised, even if the property set was called. But I still need to display such properties to the user as being "updated" from the datasource.
What is the common pattern here? Do I need another event (in addtion to property_changed) in my property setters- "property_attemptedset"?
Edit: So one suggestion is that I simply not do an equality check in the property setter, so that "property_changed" gets fired no matter what. But I've always been told to filter for actual value changes. Is that not best practice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,当您实际上没有更改属性的值时,避免引发 PropertyChanged 事件是很常见的......但没有规则说您必须这样做。只需删除 setter 中用于检查新值与当前值的任何逻辑即可。
Well, it's pretty common to avoid raising the PropertyChanged event when you're not actually changing the property's value... but there's no rule that says you have to. Just remove any logic in the setter which checks the new value against the current value.
如果您这样编写属性:
即使值相同,它也应该始终触发
PropertyChanged
事件。您不需要为此触发任何其他事件。除了您的编辑外, 编辑
:是的,检查实际值更改被认为是最佳实践,但如果您的方案需要跟踪非值更改,我不明白为什么您不能改变它。 “最佳实践”并不意味着“违反规则就进监狱”,对吗?
但是,如果这些类有任何其他观察者,它们也依赖于常见的 PropertyChanged 行为,您始终可以扩展属性并抛出另一个(自定义)事件,如您提到的:
if you write your properties like this:
It should always fire the
PropertyChanged
-event, even if the value is the same. You don't need to fire any additional events for this.EDIT
in addition to your edit: Yes, it is considered best practice to check for actual value changes, but if your scenario needs to track non-value changes, I don't see why you can't change it. "Best practice" doesn't mean "Break-this-rule-and-go-to-jail" thing right?
But in case these classes have any other observers, which also rely on the common PropertyChanged behavior, you can always extend the properties and throw another (custom) event like you mentioned:
根据您所写的内容,听起来您的下一步有时是阻止更改。如果是这样,我将引入一个新事件,例如 BeforePropertySet,并传递一个允许取消的事件对象。
这与最佳实践保持一致(防止有一天有人使用属性更改了传统方式并且无法弄清楚为什么他们的代码不起作用),澄清了您正在做的事情,并让您稍微分离了关注点更好的。
It sounds like your next step based on what you've written is to sometimes prevent the change. If so, I'd introduce a new event like BeforePropertySet, and pass an event object that allows cancelling.
This keeps with best practices (preventing the eventuality where someone some day comes along to use propertychanged the conventional way and can't figure out why their code doesn't work), clarifies what you're doing, and lets you separate concerns a bit better.