WPF 自定义路由事件

WPF 自定义路由事件

一、自定义路由事件步骤

**   ① 注册路由事件
   ② 为路由事件添加 CLR 事件包装器
   ③ 创建可激发路由事件的方法**

二、注册路由事件

EventManager.RegisterRoutedEvent(String, RoutingStrategy, Type, Type)

作用:将新的路由事件注册到 WPF 事件系统中。

参数:

String name

路由事件的名称,该名称必须唯一,且不能为 null 或空。

RoutingStrategy routingStrategy

路由策略。

Type handlerType

事件处理程序的类型。 必须为委托类型,不能为 null。

Type ownerType

路由事件的所有者类类型。 不能为 null(比如你声明一个控件,你对这个控件定义一个路由,所有者就是这个控件)

如果不需要传递参数,参数三可以使用原生的 RoutedEventHandler

csharp 复制代码
public partial class TestControl : UserControl
{
    //①声明和注册路由事件
    public static readonly RoutedEvent ClickRountEvent = EventManager.RegisterRoutedEvent("ClickRount", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TestControl ));
}

如果 RoutedEventHandler 的参数跟我们自定义的事件参数不一致,就可以使用 EventHandler 的泛型版指定我的事件参数类型固定格式如下:

csharp 复制代码
public class RoutedDelegate : RoutedEventArgs
{
    public RoutedDelegate(RoutedEvent routedEvent, object source) : base(routedEvent, source)
    {
    }
    //添加自定义参数
    public string UriMessage { get; set; }
}

然后将参数三换成 EventHandler <定义的类> 即可

csharp 复制代码
public static readonly RoutedEvent ClickRountEvent = EventManager.RegisterRoutedEvent("ClickRount", RoutingStrategy.Bubble, typeof(EventHandler<RoutedDelegate>), typeof(TestControl ));

二、为路由事件添加 CLR 事件包装器

csharp 复制代码
//为路由事件添加 CLR 事件包装器
public event RoutedEventHandler ClickRount
{
    add { AddHandler(ClickRountEvent, value); }
    remove { RemoveHandler(ClickRountEvent, value); }
}

三、创建可激发路由事件的方法

我这里使用控件触发鼠标左键按下事件来触发

csharp 复制代码
//xaml
<vlc:VlcControl x:Name="vlcPlayer" MouseLeftButtonDown="vlcPlayer_MouseLeftButtonDown"/>
csharp 复制代码
//cs
private void vlcPlayer_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
     //将需要发送的信息与路由事件关联
     RoutedDelegate routedDelegate = new RoutedDelegate(ClickRountEvent, this);
     routedDelegate.UriMessage = VideoPath;
     //调用 RaiseEvent 方法发送该事件
     RaiseEvent(routedDelegate);
 }

三、调用

当我需要调用的时候,只需要再调用的地方为该路由指定一个方法,然后满足触发该路由条件时,就会触发该方法。

csharp 复制代码
//xaml
vlcPlay:TestControl ClickRount="TestControl_ClickRount" ></vlcPlay:TestControl >
csharp 复制代码
private void TestControl_ClickRount(object sender, RoutedEventArgs e)
{
    RoutedDelegate msg = e as RoutedDelegate;
}

---自定义路由完整代码:

csharp 复制代码
public partial class TestControl : UserControl
{
   #region 声明路由
   //声明和注册路由事件
   public static readonly RoutedEvent ClickRountEvent = EventManager.RegisterRoutedEvent("ClickRount", RoutingStrategy.Bubble, typeof(EventHandler<RoutedDelegate>), typeof(TestControl ));

   //为路由事件添加 CLR 事件包装器
   public event RoutedEventHandler ClickRount
   {
       add { AddHandler(ClickRountEvent, value); }
       remove { RemoveHandler(ClickRountEvent, value); }
   }

   private void vlcPlayer_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
   {
       //将需要发送的信息与路由事件关联
       RoutedDelegate routedDelegate = new RoutedDelegate(ClickRountEvent, this);
       routedDelegate.UriMessage = VideoPath;
       //调用 RaiseEvent 方法发送该事件
       RaiseEvent(routedDelegate);
   }
   #endregion
}

public class RoutedDelegate : RoutedEventArgs
{
    public RoutedDelegate(RoutedEvent routedEvent, object source) : base(routedEvent, source)
    {
    }
    //添加自定义参数
    public string UriMessage { get; set; }
}
相关推荐
code bean18 小时前
【WPF】WPF 项目实战:构建一个可增删、排序的光源类型管理界面(含源码)
wpf
沉到海底去吧Go1 天前
【图片识别改名】如何批量将图片按图片上文字重命名?自动批量识别图片文字并命名,基于图片文字内容改名,WPF和京东ocr识别的解决方案
ocr·wpf·图片识别改名·图片识别重命名·图片内容改名
lph19721 天前
自定义事件wpf
wpf
code bean1 天前
【WPF】从普通 ItemsControl 到支持筛选的 ItemsControl:深入掌握 CollectionViewSource 用法
wpf
碎碎念的安静2 天前
WPF可拖拽ListView
c#·wpf
界面开发小八哥2 天前
界面组件DevExpress WPF中文教程:Grid - 如何识别行和卡片?
.net·wpf·界面控件·devexpress·ui开发
TwilightLemon3 天前
WPF 使用CompositionTarget.Rendering实现平滑流畅滚动的ScrollViewer,支持滚轮、触控板、触摸屏和笔
wpf
Vae_Mars5 天前
WPF中自定义消息弹窗
wpf
Magnum Lehar5 天前
GameEngine游戏引擎前端界面wpf页面实现
前端·游戏引擎·wpf
TA远方5 天前
【C#】一个简单的http服务器项目开发过程详解
服务器·http·c#·wpf·web·winform·console