基础知识:

  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. ConcurrentModificationException

    //需求:如何集合中有给定的元素就在集合中在插入一个元素public class ListIteratorDemo2 { public static void main(String[] args) ...

  2. 在ASP.NET Web Application中通过SOAP协议调用Bing搜索服务

    本文介绍了如何在ASP.NET Web Application中将Bing搜索作为Web Service来使用,并通过HTTP的SOAP协议在ASP.NET Web Application中调用Bin ...

  3. itertools模块(收藏)

    Python的内建模块itertools提供了非常有用的用于操作迭代对象的函数. count().cycle().repeat() 首先,我们看看itertools提供的几个“无限”迭代器: > ...

  4. iOS开发--图片轮播

    直接上代码了,比较简单.演示下载地址:Demo // // UYViewController.m // 图片轮播器 // // Created by jiangys on 15/5/23. // Co ...

  5. 何为仿射变换(Affine Transformation)

    http://www.cnblogs.com/ghj1976/p/5199086.html 变换模型是指根据待匹配图像与背景图像之间几何畸变的情况,所选择的能最佳拟合两幅图像之间变化的几何变换模型.可 ...

  6. 怎么查看是否安装Scrapy

    1.在python shell 下输入 import scrapy

  7. 20145316许心远《网络对抗》EXP7网络欺诈技术防范

    20145316许心远<网络对抗>EXP7网络欺诈技术防范 实验后回答问题 通常在什么场景下容易受到DNS spoof攻击 公共共享网络里,同一网段可以ping通的网络非常容易被攻击 在日 ...

  8. jquery-easyui combobox combogrid 级联不可编辑实例

    jquery-easyui combobox combogrid 级联不可编辑实例 如何让jquery-easyui的combobox像select那样不可编辑?为combobox添加editable ...

  9. 远程获得的有趣的linux命令

    使用这些工具从远程了解天气.阅读资料等. 我们即将结束为期 24 天的 Linux 命令行玩具日历.希望你有一直在看,如果没有,请回到开始,从头看过来.你会发现 Linux 终端有很多游戏.消遣和奇怪 ...

  10. NetSuite助力各行业企业快速发展

    Oracle NetSuite今天发布了一系列全新技术创新,帮助各行各业企业提升收入.海外扩张以及赋能更多业务用户.最新推出的商务管理.财务管理和分析能力可协助企业利用NetSuite平台来超越客户预 ...