一。定义路由事件
接上回依赖项属性,假设我们需要一个颜色改变时触发的路由事件,先实例化路由事件类型
public static readonly RoutedEvent ColorChangeEvent;
添加事件封装器
public event RoutedEventHandler ColorChange
{
add
{
AddHandler(ColorChangeEvent, value);
}
remove
{
RemoveHandler(ColorChangeEvent, value);
}
}
在静态构造函数中注册路由事件
ColorChangeEvent = EventManager.RegisterRoutedEvent(
"ColorChange", //名称
RoutingStrategy.Bubble, //冒泡路由事件
typeof(RoutedEventHandler), //事件委托类型
typeof(MyButton));//所有类型
引发事件
RoutedEventArgs ew = new RoutedEventArgs(MyButton.ColorChangeEvent, bt);
bt.RaiseEvent(ew);//触发冒泡路由事件,bt是所有类的实例
完整代码如下
//定义路由事件(实例化路由事件类型)
public static readonly RoutedEvent ColorChangeEvent;
public static readonly RoutedEvent PreviewColorChangeEvent;
//事件封装器
public event RoutedEventHandler ColorChange
{
add
{
AddHandler(ColorChangeEvent, value);
}
remove
{
RemoveHandler(ColorChangeEvent, value);
}
}
public event RoutedEventHandler PreviewColorChange
{
add
{
AddHandler(PreviewColorChangeEvent, value);
}
remove
{
RemoveHandler(PreviewColorChangeEvent, value);
}
}
//静态构造函数中注册依赖项属性/路由事件
static MyButton()
{
ColorChangeEvent = EventManager.RegisterRoutedEvent(
"ColorChange", //名称
RoutingStrategy.Bubble, //冒泡路由事件
typeof(RoutedEventHandler), //事件委托类型
typeof(MyButton));//所有类型
PreviewColorChangeEvent = EventManager.RegisterRoutedEvent(
"PreviewColorChange", //名称
RoutingStrategy.Tunnel,//隧道路由事件
typeof(RoutedEventHandler),//事件委托类型
typeof(MyButton));//所有类型
···
}
//依赖项属性变化时回调
private static void RGBColorPropertyChanged(
DependencyObject d,DependencyPropertyChangedEventArgs e)
{
MyButton bt = d as MyButton;
if(bt!=null)
{
RoutedEventArgs es = new RoutedEventArgs(MyButton.PreviewColorChangeEvent, bt);
bt.RaiseEvent(es);//触发隧道路由事件
bt.Foreground = new SolidColorBrush(Color.FromRgb((byte)bt.RedColor, (byte)bt.GreenColor, (byte)bt.BlueColor));//修改颜色
RoutedEventArgs ew = new RoutedEventArgs(MyButton.ColorChangeEvent, bt);
bt.RaiseEvent(ew);//触发冒泡路由事件
}
}
二。共享路由事件
如果只是需要使用已有的路由事件,可以在静态构造函数中通过AddOwner函数添加:
public static readonly RoutedEvent ColorChangeEvent;
static MyButton2()
{
ColorChangeEvent = MyButton.ColorChangeEvent.AddOwner(typeof(MyButton2));
三。自定义RoutedEventArgs
先定义RoutedEventArgs的派生类,加入想要传递的数据
using System;
using System.Windows;
namespace San13DramaEditor
{
class MyRoutedEventArgs : RoutedEventArgs
{
public int redColor { get; set; }
public int greenColor { get; set; }
public int blueColor { get; set; }
public MyRoutedEventArgs(RoutedEvent routedEvent, object source,int red,int green,int blue) : base(routedEvent, source)
{
redColor = red;
greenColor = green;
blueColor = blue;
}
}
}
实例化及注册时我们修改为
public static readonly RoutedEvent ColorChangeEvent
= EventManager.RegisterRoutedEvent(
"ColorChange", //名称
RoutingStrategy.Bubble, //冒泡路由事件
typeof(EventHandler<MyRoutedEventArgs>), //事件委托泛型
typeof(MyButton));//所有类型
触发变为
MyRoutedEventArgs ew = new MyRoutedEventArgs(MyButton.ColorChangeEvent, bt, bt.RedColor, bt.GreenColor, bt.BlueColor);
bt.RaiseEvent(ew);//触发冒泡路由事件
使用时注意参数
private void MyButton_ColorChange(object sender, MyRoutedEventArgs e){}
四。总结:
1)注册路由事件时,
1.声明public static readonly RoutedEvent类型的实例
2.声明一个事件,通过AddHandler和RemoveHandler添加删除
3.静态构造函数中用EventManager.RegisterRoutedEvent注册路由事件
4.通过RaiseEvent引发路由事件
2)共享路由事件时,在使用类的静态构造函数中调用共享类的路由事件的AddOwner()方法。
3)自定义RoutedEventArgs时,先完成派生类,然后注册时选用typeof(EventHandler<……>)进行注册,并使用自定义的RoutedEventArgs引发事件,最后事件处理函数的参数必须是自定义的RoutedEventArgs。