WPF 中的路由事件 - 使用操作委托
我正在开发一个用户控件,并希望使用路由事件。我注意到提供了两个委托 - RoutedEventHandler 和 RoutedPropertyChangedEventHandler。第一个不传递任何信息,第二个获取属性更改的旧值和新值。但是,我只需要传递一条信息,因此我需要相当于一个 Action 委托。有提供什么吗?我可以使用 Action 委托吗?
I'm developing a user control, and wish to use a routed event. I notice that there are two delegates provided - RoutedEventHandler, and RoutedPropertyChangedEventHandler. The first that doesn't pass along any information, and the second that takes the old and new values of a property change. However, I need to pass just a single piece of information, so I want the equivalent of an Action delegate. Is there anything provided? Can I use an Action delegate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建 RoutedEventArgs 的子类来保存附加数据,并将
EventHandler
与您的 args 类一起使用。这将可转换为 RoutedEventHandler 并且附加数据将在您的处理程序中可用。您可以创建一个通用的 RoutedEventArgs 类来保存任何类型的单个参数,但是创建一个新类通常会使代码更易于阅读并且更易于修改以在将来包含更多参数。
Create a subclass of RoutedEventArgs to hold your additional data, and use
EventHandler<T>
with your args class. This will be convertible to RoutedEventHandler and the additional data will be available in your handlers.You could create a generic RoutedEventArgs class that holds a single parameter of any type, but creating a new class usually makes the code easier to read and easier to modify to include more parameters in the future.