当前位置: 首页 > article >正文

WPF 自定义路由事件

WPF 自定义路由事件

一、自定义路由事件步骤

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

二、注册路由事件

  EventManager.RegisterRoutedEvent(String, RoutingStrategy, Type, Type)
    作用:将新的路由事件注册到 WPF 事件系统中。
    参数:
      String name
        路由事件的名称,该名称必须唯一,且不能为 null 或空。
      RoutingStrategy routingStrategy
        路由策略。
      Type handlerType
        事件处理程序的类型。 必须为委托类型,不能为 null。
      Type ownerType
        路由事件的所有者类类型。 不能为 null(比如你声明一个控件,你对这个控件定义一个路由,所有者就是这个控件)

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

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

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

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

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

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

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

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

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

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

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

三、调用

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

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

—自定义路由完整代码:

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; }
}

http://www.kler.cn/news/315224.html

相关文章:

  • HTTP的基本格式
  • 整合多方大佬博客以及视频 一文读懂 servlet
  • go/函数
  • Java笔试面试题AI答之单元测试JUnit(5)
  • 3. 什么是连接池?为什么使用数据库连接池?
  • AgentScope中带有@功能的多Agent组对话
  • Github Wiki 超链接 转 码云Gitee Wiki 超链接
  • hcia-openEuler V1.0师资题库-试卷2
  • MySQL数据库专栏(九)内联查询
  • 如何做系统架构?从动态系统思考的角度
  • 线性规划------ + 案例 + Python源码求解(见文中)
  • idea2021git从dev分支合并到主分支master
  • C#基础(14)冒泡排序
  • Go 并发模式:扩展与聚合的高效并行
  • MATLAB中typecast函数用法
  • 【JVM】概述
  • Mac导入iPhone的照片怎么删除?快速方法讲解
  • 《中国数据库前世今生》纪录片观感:从古至今数据库的演变与未来
  • 蓝桥杯【物联网】零基础到国奖之路:八. RTC
  • ubuntu使用Vscode进行实现UDP编程
  • 深入解析ElasticSearch从基础概念到性能优化指南
  • SQL 语法学习指南
  • 如何制作ubuntu启动U盘
  • macos tcl-tk python图形库软件包安装 port 和brew 包管理工具安装方法和使用总结
  • 单核1.5 TFLOPS、ASIL-B功能安全!Imagination发布汽车GPU IP产品Imagination DXS GPU
  • 如何写一个自动化Linux脚本去进行等保测试--引言
  • 梧桐数据库(WuTongDB):Vectorized Query Optimizers 简介
  • 【C高级】有关shell脚本的一些练习
  • Vue Router 编程式导航全攻略:深入掌握 push, replace, go, back, forward,beforeEach 方法
  • presto操作hive数据的时候如何覆盖写数据