我正在尝试将事件绑定到在自定义 Form 类中创建的表单元素。我对如何绑定存在于类 FormLoader(一个 UserControl 文件)上的方法一无所知。前任:
public class FormBuilder
{
public Control Create(){
//create form controls here... for each control call AddEvent.
}
void AddEvent(object o, string eventname, string methodname)
{
EventInfo ei = o.GetType().GetEvent(eventname);
MethodInfo mi = typeof (FormLoader).GetMethod(methodname, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Default);
Delegate del = Delegate.CreateDelegate(ei.EventHandlerType, typeof (FormLoader), mi);
ei.AddEventHandler(o, del);
}}
这就是我实例化表单的方式。
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Form = new Form();
Controls.Add(Form.Create());
}
public void ProcessEventAction(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine(@"Control Internal Hit");
}
我收到“错误绑定到目标方法”。知道如何为范围外存在的事件绑定方法吗?