是否可以将一个控件多次订阅同一事件处理程序?
我想知道如果我将 TextChanged
事件处理程序绑定到 TextBox
控件,那么如何确保不允许再次绑定此事件处理程序?
I would like to know if I bind the TextChanged
event handler to a TextBox
control, then how can I ensure that won't be allowed to bind this event handler again?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法确保这一点。理论上,您可以多次将同一事件处理程序绑定到文本框(或其他控件)。事件允许您做的唯一事情是添加处理程序和删除处理程序 - 没有提供其他方法来检查现有订阅者。如果您不相信我,Jon Skeet 在此处和中提供了权威答案 他关于事件的文章。
如果您需要确保不会意外地将控件订阅同一事件两次,则需要自己对其进行跟踪。老实说,您永远不应该陷入不知道订阅了哪些事件处理程序的情况。这不仅反映了草率的设计,而且可能还意味着您在不再需要事件处理程序时没有注意删除它们。
此问题的答案中提供了可能的解决方案< /a>,但我警告你不要盲目使用这样的东西。正如其他人所说,这段代码是一种反模式。
You can't ensure that. You would theoretically be allowed to bind the same event handler to a textbox (or other control) more than once. The only thing that events allow you to do is add a handler and remove a handler—there's no additional means provided to check for existing subscribers. If you don't believe me, Jon Skeet provides the authoritative answer here, and in his article on events.
If you need to ensure that you don't accidentally subscribe a control to the same event twice, you'll need to keep track of it yourself. Honestly, you should never end up in a situation where you don't know what event handlers are subscribed. Not only does this reflect sloppy design, but it probably also means that you aren't taking care to remove your event handlers when they are no longer necessary.
A possible solution is provided in the answers to this question, but I caution you from using something like this blindly. As others have argued, this code is something of an anti-pattern.
您可以根据需要以编程方式绑定它多次。如果你想防止这种情况,你可以使用 List
You can bind it programatically as many times as you'd like. If you want to prevent this you can use a List<object> to keep references in, for example:
实际上你可以使用 hack。
确保没有其他方法绑定到文本框的 TextChanged 事件
将所有主要负载文本更改处理程序绑定到新的自定义事件,而不是直接绑定文本框 textchanged 事件
所有这些文书工作是因为事件实际上提供了大量信息,但仅限于定义它们的类内部的方法。
您可以使用检查绑定委托
及其计数
,或者仅通过 foreach 进行计数。
You can actually use a hack.
Ensure that NO OTHER METHOD is bound to TextChanged event of the text box
Bound all your main payload text changed handlers to your new custom event instead of text box textchanged event directly
All that paperwork is because events actually provide a lot of information, but only to the methods inside the class where they are defined.
You can check bound delegates with
and their count with
or just count them through foreach.