有没有办法从类静态方法中设置按钮单击路由的事件处理程序?
我有一个带有一个按钮和两个参数的UserControl项目"SendButton“:
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"); 谢谢
发布于 2017-01-06 05:48:13
用户控件的构造函数应以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);https://stackoverflow.com/questions/41495159
复制相似问题