开发者

Simple C# event args question

开发者 https://www.devze.com 2022-12-19 16:27 出处:网络
Im using WPF which has a Storyboard cla开发者_高级运维ss that has a Completed event. I use it like so:

Im using WPF which has a Storyboard cla开发者_高级运维ss that has a Completed event.

I use it like so:

sb.Completed += AddControlToTaskbar;

private void AddControlToTaskbar(object sender, EventArgs args)
{
    //...
}

How to I pass in the EventArgs to my method? Its always null, and I need it to be a custom class

Thanks Mark


You don't pass the EventArgs to your method, the framework which dispatches the event does that. A common way to handle this is to wrap up your AddControlToTaskbar method in a class which stores the state, e.g.:

sb.Completed += new MyCustomClass(theStateYouNeedToStore).AddControlToTaskbar;

Your constructor stores the state.

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.
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消