有没有办法从类静态方法设置按钮单击路由事件处理程序?
我得到了用户控件项"发送按钮",其中包含 XAML 中的按钮和两个参数:
public SendButton(string buttonText, string eventHandler)
{
InitializeComponent();
ButtonBlock.Content = buttonText;
// This is what I´ve tried
ButtonBlock.Click += (Func<RoutedEventHandler>) typeof(RoutedEvents).GetMethod(eventHandler);
// This is what I want to achieve:
// ButtonBlock.Click += RoutedEvents.WhatIsYourName();
// But it doesn´t work anyways, because of a missing arguments
}
然后是类内的静态方法
public class RoutedEvents
{
public static void WhatIsYourName(object sender, TextChangedEventArgs e)
{
//
}
}
这就是我想称呼它的方式:
new SendButton("Send", "WhatIsYourName");
谢谢
UserControl 的构造函数应将RoutedEventHandler
作为参数:
public SendButton(string buttonText, RoutedEventHandler clickHandler)
{
InitializeComponent();
ButtonBlock.Content = buttonText;
ButtonBlock.Click += clickHandler;
}
作为参数传递的处理程序方法必须具有正确的签名,第二个参数RoutedEventArgs
:
public class RoutedEvents
{
public static void WhatIsYourName(object sender, RoutedEventArgs e)
{
//
}
}
然后像这样传递它(不带括号):
new SendButton("Send", RoutedEvents.WhatIsYourName);