如何将附加本地对象变量传递给我的事件处理程序?
我想将本地对象传递给事件处理程序。我怎样才能做到这一点?例如,如何在事件处理函数“hyperlinkButton_Click”中引用在下面的主函数中声明的“graphic”对象?
void main()
{
Graphic graphic = new Graphic();
hyperlinkButton.Click+=new RoutedEventHandler(hyperlinkButton_Click);
}
void hyperlinkButton_Click(object sender, EventArgs e)
{
}
I want to a pass local object to the event handler. How can I do that? For example, how can I reference the "graphic" object, which is declared in the main function below, in the event handler function "hyperlinkButton_Click"?
void main()
{
Graphic graphic = new Graphic();
hyperlinkButton.Click+=new RoutedEventHandler(hyperlinkButton_Click);
}
void hyperlinkButton_Click(object sender, EventArgs e)
{
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用委托或 lambda 表达式。
Use a delegate or a lambda expression.
您可以尝试关闭
graphic
变量:如果您最终需要手动删除事件处理程序,这不是一个好主意。或者,您可以将
graphic
设置为字段而不是局部变量。You could try closing on the
graphic
variable:This would not be a good idea if you eventually need to remove the event handler manually. Alternatively, you could make
graphic
a field instead of a local variable.但不是好办法。
but not good way.