首先自定义事件支持事件路由,需要使用 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="350" Width="525">
<Grid>
<Button Content="获取时间" Height="29" HorizontalAlignment="Left" Margin="192,170,0,0" Name="button1" VerticalAlignment="Top" Width="100" />
<TextBox Height="58" HorizontalAlignment="Left" Margin="89,80,0,0" Name="textBox1" VerticalAlignment="Top" Width="339" />
<Label Content="自定义路由事件演示" Height="38" HorizontalAlignment="Left" Margin="141,26,0,0" Name="label1" VerticalAlignment="Top" Width="208" FontSize="22" 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);
} }
}

效果展示:

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

最新文章

  1. ABAP单元测试最佳实践
  2. Java多线程线程学习(一)
  3. POJ 2029 Get Many Persimmon Trees
  4. springMvc配置编码过滤器
  5. C#轻型ORM框架PetaPoco试水
  6. clang failed with exit code 1 的常见情况
  7. 通过 File API 使用 JavaScript 读取文件
  8. Spring Boot 集成 Mybatis
  9. c# 多线程 创建对象实例
  10. Mysql查询某字段值重复的数据
  11. centos-6.7 内核升级(转)
  12. anaconda相关使用方法
  13. 解决一个Ubuntu中编译NEON优化的OpenCV的错误
  14. JS--编码规范
  15. 实现wc部分功能 java
  16. jdk 环境
  17. rabbitmq安装及基本操作(含集群配置)
  18. 测试oracle数据库连接
  19. Django--URL(路由层)
  20. Window下mysql环境配置问题整理

热门文章

  1. WCF、WebAPI、WCFREST和Web服务的差异 ASP.NETMVC和ASP.NETWebAPI的差异
  2. Github如何回退/回滚到某个版本
  3. 计算请假天数JavaScript方法
  4. 微信小程序- 提示不在以下合法域名列表中
  5. 微软BI SSIS 2012 辅助阅读博客
  6. dyld`__abort_with_payload:
  7. JS代码把JSON字符串转换为对象,计算对象的长度并把它转换为数字类型,把转换的值相加减
  8. spring cloud配置中心属性加密处理
  9. Nginx实现url请求不区分大小写
  10. WPF显示图片