RoutedEventArgs 与 EventArgs
我正在学习 WPF / Silverlight,并在 MS 视频广播中看到现在建议使用 RoatedEventArgs
而不是 EventArgs
;虽然它没有具体说明原因。
我有一个 win 表单应用程序,它使用“小部件”接口,试图不与特定的显示技术(在 Presenters / ViewModels 中)绑定,所以如果我的 IButton Click 事件现在需要采用 RoutedEventArgs > 现在我想它没那么有用了。
有人可以解释一下我是否应该在所有情况下切换到 RoatedEventArgs
以及为什么吗?
顺便说一句,还有其他人有关于使用我所描述的界面小部件的经验/意见吗?
I am learning WPF / Silverlight and saw in an MS vidcast that it is now recommended to use RoutedEventArgs
over EventArgs
; although it didn't say exactly why.
I have a win forms app that uses interfaces for "widgets" in an attempt to not be tied to a specific display technology (in Presenters / ViewModels), so if my IButton Click event now needs to take the RoutedEventArgs
now I guess it isn't as useful.
Can someone please explain if I should switch to RoutedEventArgs
in all cases and why?
As an aside, does anyone else have experience / opinions about using interface widgets as I'm describing them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,基本上,
RoatedEvent
会遍历Logical
树,要么从源元素到根元素(Bubble
事件路由),要么不太频繁地从根元素到子级别元素(Tunnel
事件路由)。这意味着,如果您在StackPanel
内有一个Button
,那么它本身也在Grid
内;如果您在控件中定义Click
事件,它们都会触发该事件,除非其中一个处理该事件。如果事件路由是
Bubble
(命名为常规事件Click
),它将走:Button ->堆栈面板 -> Grid
如果事件路由是
Tunnel
(名为PreviewClick
),则相反:Grid ->堆栈面板 ->按钮
现在有了处理,它非常简单。如果它是
Bubble
路由并且Button
将RoulatedEventArgs.Handled
设置为 true,则StackPanel
和Grid
不会触发它。与RoatedEvent
相同,如果Grid
处理它,StackPanel
和Button
将不会触发它。简而言之,这是我的理解,为了简单起见,我避免了一些内容。
我推荐 本章以便更好地理解此 WPF 功能。
Well, basically a
RoutedEvent
travels through theLogical
tree, either from the source element to root element (Bubble
event route) or, less frequently from root element to sub levels elements (Tunnel
event route). What this means is that if you have aButton
inside of aStackPanel
, that itself is inside of aGrid
; if you define aClick
event in the controls they will all trigger it unless one of them handles it.If the event route is
Bubble
(named as a regular eventClick
), it will go:Button -> StackPanel -> Grid
If the event route is
Tunnel
(namedPreviewClick
), it will go the other way around:Grid -> StackPanel -> Button
So now with the handling, it's pretty simple. If it's a
Bubble
route and theButton
sets theRoutedEventArgs.Handled
to true, thatStackPanel
andGrid
will not trigger it. Same with theRoutedEvent
, if theGrid
handles it, theStackPanel
andButton
will not trigger it.This is my understanding in a nutshell, I avoided some stuff for simplicity.
I recommend this chapter for better understanding of this WPF feature.
RoutedEventArgs 是一种新型事件参数,它的存在是为了支持 WPF 事件模型:路由事件。很难在简短的条目中解释为什么 WPF 到底选择了这个模型或者其意义是什么,所以我将首先向您介绍一篇关于该主题的好文章。
RoutedEventArgs is a new type of event argument that exists to support the WPF model of eventing: Routed Events. It's hard to explain in a short entry why exactly WPF chose this model or what the point is so I'll start by pointing you to a good article on the subject.
假设我们有一个包含其他元素的 Button 元素,一个 StackPanel,它本身包含一个 TextBox 和一个 Image 元素。
Button 元素应该能够处理单击事件,无论单击的是 Image 还是 TextBox。
因此,WPF 提供了一种方法:
从源元素(这里说的是图像),到更高级别的根元素(例如这里的按钮)。
Say we have a Button element containing other elements, a StackPanel, itself containing a TextBox and an Image element.
The Button element should be able to handle a click event no matter if the Image that got clicked or if the TextBox was.
Hence WPF provides a way to :
from the source element (here say the Image), to a higher level toward root element (for instance the button here).