<i id='cAMIf'><tr id='cAMIf'><dt id='cAMIf'><q id='cAMIf'><span id='cAMIf'><b id='cAMIf'><form id='cAMIf'><ins id='cAMIf'></ins><ul id='cAMIf'></ul><sub id='cAMIf'></sub></form><legend id='cAMIf'></legend><bdo id='cAMIf'><pre id='cAMIf'><center id='cAMIf'></center></pre></bdo></b><th id='cAMIf'></th></span></q></dt></tr></i><div id='cAMIf'><tfoot id='cAMIf'></tfoot><dl id='cAMIf'><fieldset id='cAMIf'></fieldset></dl></div>

      1. <legend id='cAMIf'><style id='cAMIf'><dir id='cAMIf'><q id='cAMIf'></q></dir></style></legend>

          <bdo id='cAMIf'></bdo><ul id='cAMIf'></ul>

      2. <small id='cAMIf'></small><noframes id='cAMIf'>

      3. <tfoot id='cAMIf'></tfoot>

        如何删除所有 Click 事件处理程序?

        时间:2023-09-15
      4. <legend id='eKRVw'><style id='eKRVw'><dir id='eKRVw'><q id='eKRVw'></q></dir></style></legend>
        <tfoot id='eKRVw'></tfoot>

          • <bdo id='eKRVw'></bdo><ul id='eKRVw'></ul>
            1. <small id='eKRVw'></small><noframes id='eKRVw'>

                  <tbody id='eKRVw'></tbody>
                  <i id='eKRVw'><tr id='eKRVw'><dt id='eKRVw'><q id='eKRVw'><span id='eKRVw'><b id='eKRVw'><form id='eKRVw'><ins id='eKRVw'></ins><ul id='eKRVw'></ul><sub id='eKRVw'></sub></form><legend id='eKRVw'></legend><bdo id='eKRVw'><pre id='eKRVw'><center id='eKRVw'></center></pre></bdo></b><th id='eKRVw'></th></span></q></dt></tr></i><div id='eKRVw'><tfoot id='eKRVw'></tfoot><dl id='eKRVw'><fieldset id='eKRVw'></fieldset></dl></div>
                • 本文介绍了如何删除所有 Click 事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  可能重复:这怎么可能删除按钮的 Click 事件的所有事件处理程序?

                  我想从按钮中删除所有单击事件处理程序.我在 Stack Overflow 问题 How to remove all event handlers from a control 中找到了这种方法.

                  I want to remove all click event handlers from a button. I found this method in Stack Overflow question How to remove all event handlers from a control.

                  private void RemoveClickEvent(Button b)
                  {
                      FieldInfo f1 = typeof(Control).GetField("EventClick",
                                                              BindingFlags.Static |
                                                              BindingFlags.NonPublic);
                      object obj = f1.GetValue(b);
                      PropertyInfo pi = b.GetType().GetProperty("Events",
                                                                BindingFlags.NonPublic |
                                                                BindingFlags.Instance);
                      EventHandlerList list = (EventHandlerList)pi.GetValue(b, null);
                      list.RemoveHandler(obj, list[obj]);
                  }
                  

                  但是这一行总是返回null:

                  But this line always returns null:

                    typeof(Control).GetField("EventClick", BindingFlags.Static | BindingFlags.NonPublic);
                  

                  而且这个方法是2006年写的.

                  And this method was written in 2006.

                  这个方法有最新版本吗?

                  Is there any latest version of this method?

                  注意:我正在使用 WPF 和 .NET 4.0.

                  Note: I am working with WPF and .NET 4.0.

                  推荐答案

                  以下是一个有用的实用方法,用于检索任何路由事件的所有订阅事件处理程序:

                  The below is a helpful utility method for retrieving all subscribed event handlers for any routed event:

                  /// <summary>
                  /// Gets the list of routed event handlers subscribed to the specified routed event.
                  /// </summary>
                  /// <param name="element">The UI element on which the event is defined.</param>
                  /// <param name="routedEvent">The routed event for which to retrieve the event handlers.</param>
                  /// <returns>The list of subscribed routed event handlers.</returns>
                  public static RoutedEventHandlerInfo[] GetRoutedEventHandlers(UIElement element, RoutedEvent routedEvent)
                  {
                      // Get the EventHandlersStore instance which holds event handlers for the specified element.
                      // The EventHandlersStore class is declared as internal.
                      var eventHandlersStoreProperty = typeof(UIElement).GetProperty(
                          "EventHandlersStore", BindingFlags.Instance | BindingFlags.NonPublic);
                      object eventHandlersStore = eventHandlersStoreProperty.GetValue(element, null);
                  
                      // Invoke the GetRoutedEventHandlers method on the EventHandlersStore instance 
                      // for getting an array of the subscribed event handlers.
                      var getRoutedEventHandlers = eventHandlersStore.GetType().GetMethod(
                          "GetRoutedEventHandlers", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                      var routedEventHandlers = (RoutedEventHandlerInfo[])getRoutedEventHandlers.Invoke(
                          eventHandlersStore, new object[] { routedEvent });
                  
                      return routedEventHandlers;
                  }
                  

                  使用上述方法,您的方法的实现变得非常简单:

                  Using the above, the implementation of your method becomes quite simple:

                  private void RemoveClickEvent(Button b)
                  {
                      var routedEventHandlers = GetRoutedEventHandlers(b, ButtonBase.ClickEvent);
                      foreach (var routedEventHandler in routedEventHandlers)
                          b.Click -= (RoutedEventHandler)routedEventHandler.Handler;
                  }
                  

                  这篇关于如何删除所有 Click 事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:我可以访问内部类中的外部类对象吗 下一篇:我是否应该实例化一个新的委托?

                  相关文章

                • <small id='7bhBG'></small><noframes id='7bhBG'>

                    <bdo id='7bhBG'></bdo><ul id='7bhBG'></ul>
                • <tfoot id='7bhBG'></tfoot>
                  <i id='7bhBG'><tr id='7bhBG'><dt id='7bhBG'><q id='7bhBG'><span id='7bhBG'><b id='7bhBG'><form id='7bhBG'><ins id='7bhBG'></ins><ul id='7bhBG'></ul><sub id='7bhBG'></sub></form><legend id='7bhBG'></legend><bdo id='7bhBG'><pre id='7bhBG'><center id='7bhBG'></center></pre></bdo></b><th id='7bhBG'></th></span></q></dt></tr></i><div id='7bhBG'><tfoot id='7bhBG'></tfoot><dl id='7bhBG'><fieldset id='7bhBG'></fieldset></dl></div>

                    1. <legend id='7bhBG'><style id='7bhBG'><dir id='7bhBG'><q id='7bhBG'></q></dir></style></legend>