什么是路由事件以及它与普通事件的区别 [英] What are routed events and how it's different from normal events

查看:30
本文介绍了什么是路由事件以及它与普通事件的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人能用一个简单的例子来解释,我将不胜感激.

I will appreciate if some body can explain with a simple example.

推荐答案

想象一个包含密集子控件层次结构的窗口.现在假设您想要做某事,您可以在窗口中的任意位置单击鼠标右键.

Imagine a Window containing a dense hierarchy of child controls. Now let's say you want to do something, there's a right click anywhere in your window.

  • 对于普通事件,您必须为所有控件处理 Click 事件,因为您不确定用户可能点击的位置.
  • 使用 WPF 的路由事件,事件要么冒泡",要么隧道"(即向上或向下移动 UI 树),如果他们没有找到在当前级别处理"它的事件处理程序.所以你可以为窗口的事件编写一个处理程序,即 TopLevel.(WPF 有一个事件对的约定,PreviewXXX 和 XXX - PreviewXXX 事件首先触发并从根向下隧道传输到接收刺激的控制,然后对应的 XXX 事件从子控件向上冒泡返回到 Root).因此,如果您右键单击一个按钮,WPF 会在 UI 层次结构中向上移动,调用它找到的所有处理程序(除非有人在事件参数中标记该事件已处理".)
  • With normal events, you'd have to handle a Click event for all controls, because you're not sure where the user might click.
  • With WPF's routed events, the events either "bubble" or "tunnel" (i.e travel up the UI tree or down) if they dont find an event handler, which "handles" it at the current level. So you could write one handler for the window's event i.e. TopLevel. (WPF has a convention of event pairs, PreviewXXX and XXX - the PreviewXXX event fires first and tunnels down from root to control which received the stimulus and the counterpart XXX event then bubbles up from child control back upto Root). So if you right click a button, WPF travels up the UI hierarchy, invoking all handlers that it finds (unless someone marks the event has "handled" in the event args.)

这篇关于什么是路由事件以及它与普通事件的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆