基础知识:

  1. 传统的事件模型中,会在消息触发时将消息通过事件传给事件的订阅者(显式的事件订阅),事件订阅者使用事件处理程序来做出响应。事件订阅者必须能够直接访问到事件的宿主(拥有者)。

  2. 路由事件的事件的拥有者和事件的订阅者之间没有显式订阅关系。拥有者只负责触发事件,它并不知道事件将会由谁响应,事件的订阅者通过事件监听器监听事件,一旦事件触发就对其进行处理(调用相关的事件处理程序),同时并决定该事件是否继续传递。

  3. 传统事件通过.NET事件封装器触发,而路由事件则通过RaiseEvent()方法触发。

传统事件的参数类型为EventArgs及其子类,而路由事件则是RoutedEventArgs及其子类;


图1   EventArgs及其子类继承关系

  1. 路由事件使用EventManager.RegisterRoutedEvent()方法注册。
  2. 路由事件同依赖属性一样,也可以共享(通过routedEvent.AddOwner()添加)。
  3. 路由事件出现的三种方式:①直接路由事件(Direct Event),如MouseEnter、②冒泡路由事件(Bubbling Event),如MouseDown和③隧道路由事件(Tunneling Event),如PreviewKeyDown。当注册事件时,会传递一个RoutingStrategy枚举值指定事件行为。
  4. 通过AddHandler()方法可以继续响应被标记为已处理的事件。
  5. 隧道路由事件一般会以单词Preview开发。
  6. Focusable属性定义在UIElement类中。
  7. 自定义路由事件示例:

  WPF代码部分:

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication16"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="WpfApplication16.MainWindow"
Title="Routed Event" x:Name="window_1" Height="300" Width="300">
<Grid x:Name="grid_1" local:TimeButton.ReportTime="ReportTimeHandler">
<Grid x:Name="grid_2" local:TimeButton.ReportTime="ReportTimeHandler">
<Grid x:Name="grid_3" local:TimeButton.ReportTime="ReportTimeHandler">
<StackPanel x:Name="stackPanel_1" local:TimeButton.ReportTime="ReportTimeHandler">
<ListBox x:Name="listBox"/>
<local:TimeButton x:Name="timeButton" Width="80" Height="80" Content="Telling Time" ReportTime="ReportTimeHandler"/>
</StackPanel>
</Grid>
</Grid>
</Grid>
</Window>

C#代码部分:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace WpfApplication16
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void ReportTimeHandler(object sender, ReportTimeEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
string timeStr = e.ClickTime.ToLongTimeString();
string content = string.Format("{0} to {1}", timeStr, element.Name);
this.listBox.Items.Add(content); if (element == this.grid_2)
{
e.Handled = true;
}
}
} class ReportTimeEventArgs : RoutedEventArgs
{
public ReportTimeEventArgs(RoutedEvent routedEvent, object source)
: base(routedEvent, source)
{ } public DateTime ClickTime { get; set; }
} class TimeButton : Button
{
public static readonly RoutedEvent ReportTimeEvent = EventManager.RegisterRoutedEvent("ReportTime", RoutingStrategy.Bubble, typeof(EventHandler<ReportTimeEventArgs>), typeof(TimeButton)); public event RoutedEventHandler ReportTime
{
add { this.AddHandler(ReportTimeEvent, value); }
remove { this.RemoveHandler(ReportTimeEvent, value); }
} protected override void OnClick()
{
base.OnClick(); ReportTimeEventArgs args = new ReportTimeEventArgs(ReportTimeEvent, this);
args.ClickTime = DateTime.Now;
this.RaiseEvent(args);
}
}
}

效果:

图2  路由事件效果

WPF基础学习笔记整理 (六) RoutedEvent路由事件的更多相关文章

  1. WPF基础学习笔记整理 (八) 命令

    基础知识: 命令是应用程序的任务,并跟踪任务是否能够被执行. 命令不包含执行应用程序任务的代码. 命令是比事件更高级的元素.默认的命令目标是当前获得焦点的元素. 良好的Win应用程序,应用程序逻辑不应 ...

  2. WPF基础学习笔记整理 (九) 资源

    基础知识: WPF的资源是一种保管一系列有用对象的简单方法,方便于重用. WPF UI元素的Resources属性,都是继承自FrameworkElement列,且其类型为ResourceDictio ...

  3. WPF基础学习笔记整理 (七) Binding绑定

    基础知识: 数据绑定是一种关系,该关系告诉WPF从源对象提取一些信息,并用这些信息设置目标对象的属性:目标对象始终是依赖属性,而源对象则可以是任何内容. BindingOperations类,提供静态 ...

  4. WPF基础学习笔记整理 (五) DependencyObject & DependencyProperty

    参考资料: 1.http://www.cnblogs.com/Zhouyongh/archive/2009/10/20/1586278.html 基础知识: DependencyObject & ...

  5. WPF基础学习笔记整理 (二) XAML

    基础知识: XAML:Extensible Application Markup Language, zammel: 用于实例化.NET对象的标记语言: XMAL使用树形逻辑结构描述UI: BAML: ...

  6. WPF基础学习笔记整理 (一)

    基础知识: WPF:Windows Presentation Foundation,用于Windows的现代图形显示系统: WPF用于编写应用程序的表示层: 引入“内置硬件加速”和“分辨率无关”: S ...

  7. WPF基础学习笔记整理 (四) 布局

    WPF使用的是容器(container)进行布局: WPF窗口(Window类型)只能包含单个元素,故为了放置多个元素并增强界面效果,引入了容器: WPF布局容器都派生自System.Windows. ...

  8. WPF基础学习笔记整理 (三) x命名空间

    “x命名空间”中x是XAML的首字母,用来引导XAML编译器把XAML代码编译成CLR代码.下边的图片表格列举了该命名空间部分成员及其作用,更多请见URL:https://msdn.microsoft ...

  9. Vue1.0基础学习笔记整理

    最近一直在使用Vue.js开发项目,现将在学习过程中遇到的一些学习小细节总结如下: 1.只处理单次插值,今后的数据变化就不会再引起插值更新了 <span>This will never c ...

随机推荐

  1. (C#) SQLite数据库连接字符串

    最常用的:Data Source=filename;Version=3; 自增主键: Create  test1( [id] integer PRIMARY KEY AUTOINCREMENT ,[n ...

  2. ajax课2JSON

    1.ajax优点: a.页面无刷新 b.用户体验度较好,不会打断用户操作 c.按需求获取数据,不需要返回一个完整的页面 d.是标准的技术,不需要安装任何的插件 应用场景:注册.表格数据的增删改 2.J ...

  3. Leetcode: Reorder List && Summary: Reverse a LinkedList

    Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do th ...

  4. ACM 未解决的问题

    还没有搞定的ACM问题列表. google code jam Round1A Round1B Round1C Round2 Round3 Onsite Finals 百度之星 一.资格赛题目: dis ...

  5. rpm服务的独立服务管理

    /etc/init.d  启动脚本的位置 /etc/sysconfig/ 初始化环境配置文件 /etc/   配置文件位置 /etc/xinetd.conf  xinetd配置文件 /etc/xine ...

  6. mysql表空间文件

    1.共享表空间文件.默认表空间文件是ibdata1,大小为10M,且可拓展.共享表空间可以由多个文件组成,一个表可以跨多个文件而存在,共享表空间的最大值限制是64T. 2.独立表空间文件.独立表空间只 ...

  7. 5Lambda表达式

    C++11中的Lambda表达式用于定义并创建匿名的函数对象,以简化编程工作.首先看一下Lambda表达式的基本构成: [函数对象参数](操作符重载函数参数)mutable或exception -&g ...

  8. 【Redis学习之九】Redis集群:Twemproxy和HA

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 redis-3.0.4 主从模式对写压力没有分担,解决思路就 ...

  9. 获取Android设备的唯一识别码|设备号|序号|UUID

    如何获取一个能唯一标识每台Android设备的序号? 这个问题有很多答案,但是他们中的大部分只在某些情况下有效. 根据测试: 所有的设备都可以返回一个 TelephonyManager.getDevi ...

  10. Js基础知识7-JavaScript所有内置对象属性和方法汇总

    对象什么的,程序员可是有很多呢... JS三大对象 对象,是任何一个开发者都无法绕开和逃避的话题,她似乎有些深不可测,但如此伟大和巧妙的存在,一定值得你去摸索.发现.征服. 我们都知道,JavaScr ...