有没有办法从类静态方法设置按钮单击路由事件处理程序?
我有一个带有XAML按钮的UserControl项“SendButton”和两个参数:
public void processEvent(Event e) {
save(e);
if (e != "foo") {
broadcast(e);
}
}
然后在类
中使用静态方法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)
{
//
}
}
谢谢
答案 0 :(得分:3)
UserControl的构造函数应该以{{1}}为参数:
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)
{
//
}
}