我有一个函数,void Validate()
,它包含我对一个窗口的所有验证逻辑。
我不能简单地将其注册为事件处理程序,因为它不接受事件处理程序委托(delegate)所需的参数。此外,不同类型的控件具有不同的签名,因此我不能只让 Validate
匹配一个签名而忽略它们的内容。
这是我设置的一个小例子
txt1.TextChanged += Validate_TextChange;
password1.PasswordChanged += Validate_RoutedEvent;
txt2.TextChanged += Validate_TextChange;
txt3.TextChanged += Validate_TextChange;
password2.PasswordChanged += Validate_RoutedEvent;
txt4.TextChanged += Validate_TextChange;
void Validate_RoutedEvent(object sender, RoutedEventArgs e)
{
ValidateOptions();
}
void Validate_TextChange(object sender, TextChangedEventArgs e)
{
ValidateOptions();
}
public void ValidateOptions()
{
//Actual validation here
}
这仅显示了 2 个示例,更多控件可以具有更多签名。在我不关心传递的参数的情况下,是否有更好的方法让所有事件处理程序调用一个函数?
编辑: 我喜欢 Jon 提出的添加参数并简单地忽略它们的选项。这确实解决了大部分问题,但任何时候我想直接调用这个函数,比如手动触发验证,我都必须包含虚拟参数以满足编译器的要求。 ValidateOptions(this, new EventArgs())
Dan 提出的使用匿名函数的建议可以解决这个问题,但在关联事件处理程序时并不那么清晰。
这两种解决方案似乎都不能让您在忽略签名的同时将函数注册为事件处理程序,同时还保留调用函数而不创建伪参数的能力,但有多种方法可以非常接近。
编辑:
这是更新后的示例,它实现了 Jon 的通用事件处理解决方案,但保留了一个可以直接调用的 0 参数函数
txt1.TextChanged += ValidationEvent;
password1.PasswordChanged += ValidationEvent;
txt2.TextChanged += ValidationEvent;
txt3.TextChanged += ValidationEvent;
password2.PasswordChanged += ValidationEvent;
txt4.TextChanged += ValidationEvent;
//Single event handler accepting EventArgs, which is the base class
//for all more-specific event classes
void ValidationEvent(object sender, EventArgs e)
{
//Ignores arguments and calls 0 argument ValidateOptions
ValidateOptions();
}
//0 argument function that performs actual validation and can be called
//directly from other functions without the need to pass in a fake sender
//and eventargs parameter
public void ValidateOptions()
{
//Actual validation here
}
最佳答案
您可以有一个忽略参数的方法:
public void ValidateOptions(object sender, EventArgs e)
{
}
编译器将允许从ValidateOptions
方法组转换为遵循正常事件模式的任何 委托(delegate)类型,这样第一个参数就是sender
第二个参数是从 EventArgs
派生的某种类型。
关于c# - 为具有不同事件处理程序委托(delegate)的多个控件实现单个事件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8190453/