使用不同的事件处理程序委托为多个控件实现单个事件处理程序

Eri*_*ric 6 c# validation wpf event-handling

我有一个函数,void Validate()它包含窗口的所有验证逻辑.

我不能简单地将其注册为事件处理程序,因为它不接受事件处理程序委托所需的参数.另外,不同类型的控件具有不同的签名,因此我不能只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
}
Run Code Online (Sandbox Code Playgroud)

这仅显示了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
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 7

可以使用一个忽略参数的方法:

public void ValidateOptions(object sender, EventArgs e)
{
}
Run Code Online (Sandbox Code Playgroud)

编译器将允许从ValidateOptions方法组到正常事件模式之后的任何委托类型的转换,使得第一个参数是sender,第二个参数是从中派生的某个类型EventArgs.

  • @HB:是的 - 我更愿意明白它是在那里进行事件处理:) (2认同)

Dan*_*ant 5

您可以使用lambda表达式来减少为包装器编写的代码量,但是,在幕后,它仍然在创建包装器方法来丢弃参数.

txt3.TextChanged += (s,e) => ValidateOptions();
password2.PasswordChanged += (s,e) => ValidateOptions();
txt4.TextChanged += (s,e) => ValidateOptions();
Run Code Online (Sandbox Code Playgroud)