RoutedEventArgs 与 EventArgs

发布于 2024-08-05 00:16:11 字数 372 浏览 3 评论 0原文

我正在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏有森光若流苏 2024-08-12 00:16:11

好吧,基本上,RoatedEvent 会遍历Logical 树,要么从源元素到根元素(Bubble 事件路由),要么不太频繁地从根元素到子级别元素(Tunnel 事件路由)。这意味着,如果您在 StackPanel 内有一个 Button,那么它本身也在 Grid 内;如果您在控件中定义Click事件,它们都会触发该事件,除非其中一个处理该事件。

如果事件路由是Bubble(命名为常规事件Click),它将走:

Button ->堆栈面板 -> Grid

如果事件路由是Tunnel(名为PreviewClick),则相反:

Grid ->堆栈面板 ->按钮

现在有了处理,它非常简单。如果它是 Bubble 路由并且 ButtonRoulatedEventArgs.Handled 设置为 true,则 StackPanelGrid 不会触发它。与RoatedEvent相同,如果Grid处理它,StackPanelButton将不会触发它。

简而言之,这是我的理解,为了简单起见,我避免了一些内容。

我推荐 本章以便更好地理解此 WPF 功能。

Well, basically a RoutedEvent travels through the Logical 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 a Button inside of a StackPanel, that itself is inside of a Grid; if you define a Click 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 event Click), it will go:

Button -> StackPanel -> Grid

If the event route is Tunnel (named PreviewClick), 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 the Button sets the RoutedEventArgs.Handled to true, that StackPanel and Grid will not trigger it. Same with the RoutedEvent, if the Grid handles it, the StackPanel and Button 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.

有深☉意 2024-08-12 00:16:11

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.

迷途知返 2024-08-12 00:16:11

假设我们有一个包含其他元素的 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 :

  • Propagate an event through element tree most of the time bubbling
    from the source element (here say the Image), to a higher level toward root element (for instance the button here).
  • Handle such a propagated event.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文