【问题标题】:How to create dynamic control event handler如何创建动态控件事件处理程序
【发布时间】:2009-11-03 11:26:30
【问题描述】:

我有控制事件处理程序的问题。我创建了一个控件(按钮),我想用一个方法绑定点击事件。但我有“错误绑定到目标方法”。例外。

代码是,

class SetControlEvent
{

    public void Sleep()
    {
        System.Threading.Thread.Sleep(5000);
    }

    internal void Set(object theObject,XmlNode theControlNode)
    {
        EventInfo ei = theObject.GetType().GetEvent("Click");


        EventDescriptorCollection events = TypeDescriptor.GetEvents(theObject);
        foreach (EventDescriptor theEvent in events)
        {
            foreach (XmlAttribute attribute in theControlNode.Attributes)
            {
                if (theEvent.DisplayName == attribute.Name)
                {

                    MethodInfo mi = typeof(SetControlEvent).GetMethod("Sleep");
                    Delegate del = Delegate.CreateDelegate(ei.EventHandlerType, this, "Sleep");

                    theEvent.AddEventHandler(theObject, del);



                    break;

                }
            }
        }
    }

}

那么,我该怎么办?

谢谢...

【问题讨论】:

    标签: c# dynamic controls event-handling


    【解决方案1】:

    您的 Sleep 方法签名不适合 EventHandler 委托。

    试试这个:

        public void Sleep(Object sender, EventArgs e)
        {
            System.Threading.Thread.Sleep(5000);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-03
      • 1970-01-01
      • 2013-12-24
      • 2019-04-13
      相关资源
      最近更新 更多