Mar*_*ark 1 c# events delegates
我使用WPF,它有一个Storyboard有Completed事件的类.
我这样使用它:
sb.Completed += AddControlToTaskbar;
private void AddControlToTaskbar(object sender, EventArgs args)
{
//...
}
Run Code Online (Sandbox Code Playgroud)
如何将传递EventArgs给我的方法?它总是为null,我需要它成为一个自定义类
谢谢马克
您没有传递EventArgs给您的方法,调度事件的框架就是这样做的.处理此问题的常用方法是将AddControlToTaskbar方法包装在存储状态的类中,例如:
sb.Completed += new MyCustomClass(theStateYouNeedToStore).AddControlToTaskbar;
Run Code Online (Sandbox Code Playgroud)
您的构造函数存储状态.
class MyCustomClass<T> {
private T state;
public MyCustomClass(T state) {
this.state = state;
}
public void AddControlToTaskbar(object sender, EventArgs args) {
// Do what you need to do, you have access to your state via this.state.
}
}
Run Code Online (Sandbox Code Playgroud)