如何:创建自定义路由事件

首先自定义事件支持事件路由,需要使用 RegisterRoutedEvent 方法注册 RoutedEvent

C#语法
public static RoutedEvent RegisterRoutedEvent(
string name,
RoutingStrategy routingStrategy,
Type handlerType,
Type ownerType
)

参数

name
类型:System.String 路由事件的名称。该名称在所有者类型中必须是唯一的,并且不能为 null 或空字符串。
routingStrategy
类型:System.Windows.RoutingStrategy 作为枚举值的事件的路由策略。
handlerType
类型:System.Type 事件处理程序的类型。该类型必须为委托类型,并且不能为 null。
ownerType
类型:System.Type 路由事件的所有者类类型。该类型不能为 null。
下面我们通过示例展示自定义路由事件:

先看下xaml文件,我们可以看到button中并没有click事件

<Window x:Class="简单自定义路由事件.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="" Width="">
<Grid>
<Button Content="获取时间" Height="" HorizontalAlignment="Left" Margin="192,170,0,0" Name="button1" VerticalAlignment="Top" Width="" />
<TextBox Height="" HorizontalAlignment="Left" Margin="89,80,0,0" Name="textBox1" VerticalAlignment="Top" Width="" />
<Label Content="自定义路由事件演示" Height="" HorizontalAlignment="Left" Margin="141,26,0,0" Name="label1" VerticalAlignment="Top" Width="" FontSize="" Background="BlanchedAlmond" />
</Grid>
</Window>

后台代码简单展示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 简单自定义路由事件
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent(); this.button1.AddHandler(Button.ClickEvent,//处理的事件
new RoutedEventHandler(RoutedEvent)); //事件委托 this.MEvent += new RoutedEventHandler(MainWindow_MEvent); } void MainWindow_MEvent(object sender, RoutedEventArgs e)
{
this.textBox1.Text = System.DateTime.Now.ToString();
} private static readonly RoutedEvent MyEvent = EventManager.RegisterRoutedEvent("Event",//路由事件的名称
RoutingStrategy.Direct,//事件的路由策略
typeof(RoutedEvent), //事件处理程序的类型。该类型必须为委托类型
typeof(RoutedEventArgs));//路由事件的所有者类类型 //事件访问器,进行事件提供添加和移除。
public event RoutedEventHandler MEvent
{
add
{
AddHandler(MyEvent, value);
}
remove
{
RemoveHandler(MyEvent, value);
}
}
void RoutedEvent(object o, RoutedEventArgs e)
{
//关联路由事件
RoutedEventArgs args = new RoutedEventArgs(MyEvent);
this.RaiseEvent(args);
} }
}

效果展示:


上面详细的说明建自定义路由事件的基本原理的原理,我们可以通过这样的方式来实现自己的路由事件,这样的代码移致性很高,我们可以需要的时候直接移植.

示例下载:http://files.cnblogs.com/BABLOVE/%E7%AE%80%E5%8D%95%E8%87%AA%E5%AE%9A%E4%B9%89%E8%B7%AF%E7%94%B1%E4%BA%8B%E4%BB%B6.rar

WPF 自定义路由事件的更多相关文章

  1. WPF:自定义路由事件的实现

    路由事件通过EventManager,RegisterRoutedEvent方法注册,通过AddHandler和RemoveHandler来关联和解除关联的事件处理函数:通过RaiseEvent方法来 ...

  2. WPF自定义路由事件(二)

    WPF中的路由事件 as U know,和以前Windows消息事件区别不再多讲,这篇博文中,将首先回顾下WPF内置的路由事件的用法,然后在此基础上自定义一个路由事件. 1.WPF内置路由事件 WPF ...

  3. 细说WPF自定义路由事件

    WPF中的路由事件 as U know,和以前Windows消息事件区别不再多讲,这篇博文中,将首先回顾下WPF内置的路由事件的用法,然后在此基础上自定义一个路由事件. 1.WPF内置路由事件   W ...

  4. Wpf自定义路由事件

    创建自定义路由事件大体可以分为三个步骤: ①声明并注册路由事件. ②为路由事件添加CLR事件包装. ③创建可以激发路由事件的方法. 以ButtonBase类中代码为例展示这3个步骤: public a ...

  5. WPF自定义路由事件(一)

    首先自定义事件支持事件路由,需要使用 RegisterRoutedEvent 方法注册 RoutedEvent C#语法 public static RoutedEvent RegisterRoute ...

  6. WPF 自定义路由事件 与 附加路由事件

    为student添加附件事件

  7. WPF自学入门(四)WPF路由事件之自定义路由事件

    在上一遍博文中写到了内置路由事件,其实除了内置的路由事件,我们也可以进行自定义路由事件.接下来我们一起来看一下WPF中的自定义路由事件怎么进行创建吧. 创建自定义路由事件分为3个步骤: 1.声明并注册 ...

  8. WPF路由事件三:自定义路由事件

    与依赖项属性类似,WPF也为路由事件提供了WPF事件系统这一组成.为一个类型添加一个路由事件的方式与为类型添加依赖项属性的方法类似,添加一个自定义路由事件的步骤: 一.声明路由事件变量并注册:定义只读 ...

  9. WPF的路由事件、冒泡事件、隧道事件(预览事件)

    本文摘要: 1:什么是路由事件: 2:中断事件路由: 3:自定义路由事件: 4:为什么需要自定义路由事件: 5:什么是冒泡事件和预览事件(隧道事件): 1:什么是路由事件 WPF中的事件为路由事件,所 ...

随机推荐

  1. SDK更新太慢

    同时,更新ADT和SDK Manager 在SDK Manager下Tools->Options打开了SDK Manager的Settings,选中“Force https://… source ...

  2. [C#] 常用工具类——系统日志类

    using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; namespa ...

  3. C和指针 (pointers on C)——第四章:语句(上)

    第四章--语句(上) 总结总结!!! C没有布尔类型,所以在一些逻辑推断时候必须用整型表达式,零值为假,非零值为真. for比while把控制循环的表达式收集起来放在一个地方,以便寻找. do语句比w ...

  4. MapReduce中的map个数

    在map阶段读取数据前,FileInputFormat会将输入文件分割成split.split的个数决定了map的个数.影响map个数(split个数)的主要因素有: 1) 文件的大小.当块(dfs. ...

  5. Regular Expressions in Grep Command with 10 Examples --reference

    Regular expressions are used to search and manipulate the text, based on the patterns. Most of the L ...

  6. 5 Ways to Use Log Data to Analyze System Performance--reference

    Recently we looked across some of the most common behaviors that our community of 25,000 users looke ...

  7. Android WebView的loadData方法注意事项

    loadData()中的html data中不能包含'#', '%', '\', '?'四中特殊字符,出现这种字符就会出现解析错误,显示找不到网页还有部分html代码.需要如何处理呢?我们需要用Url ...

  8. System Operations on AWS - Lab 4W - Monitoring (Windows)

    创建Web Server实例,配置CloudWatch来收集Web Server的系统日志,当错误登录次数达到设定值时触发报警 1. 创建Web Server 1.1 创建一个IAM策略 1.2 创建 ...

  9. ASP.Net免费发送短信-阿里大鱼短信接口

    有点短信余额 不用白不用 3月1号就过期了 情人节做了个免费发短信的 http://love.issuc.com/ 固定短信模板 [活动验证]您正在参加XXX的OOO活动,请确认系本人申请.需要的可以 ...

  10. [Mime] MediaTypes--电子邮件类型类 (转载)

    点击下载 MediaTypes.rar 这个类是关于 电子邮件类型类的操作,在发送电子邮件是规定以什么样的格式发送,Xml,HTML,文本等方式1.电子邮件类型帮助类,Xml格式,HTML格式等看下面 ...