WPF自定义路由事件

与依赖项属性类似,WPF也为路由事件提供了WPF事件系统这一组成。为一个类型添加一个路由事件的方式与为类型添加依赖项属性的方法类似,添加一个自定义路由事件的步骤:

一、声明路由事件变量并注册

定义只读的静态变量字段RouteEvent类来声明一个变量,然后使用EventManager的RegisterRoutedEvent()方法向事件系统注册路由事件,该方法的签名如下:

public static RoutedEvent RegisterRoutedEvent(string name, RoutingStrategy routingStrategy, Type handlerType, Type ownerType); 

该方法带有四个参数:

  • 第一个参数name表示该路由事件在WPF事件系统中的名称。
  • 第二个参数routingStrategy是RoutingStrategy类型的枚举值,标明了路由事件的路由策略,共三种策略:

    • 第一种Bubble是冒泡策略,这种模式是从触发点向根节点传递,直到最外层。
    • 第二种是Direct就是传统的事件一样的。
    • 第三种是隧道策略,这和冒泡策略相反,向下传递。
  • 第三个参数handlerType用来标明事件处理函数的类型。
  • 第四个个参数ownerType则用来标明拥有该路由事件的类型。

EventManager的RegisterRoutedEvent()方法返回一个RoutedEvent类型的实例。一般情况下,该实例将由一个public static readonly字段所保存。

二、通过标准的.NET事件包装路由事件

事件包装器使用AddHandler方法来添加路由事件的调用程序,然后使用RemoveHandler来删除已经添加的调用程序。

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

演示创建自定义路由事件:

1、新建用户控件,添加一个Button按钮,添加按钮的Click事件,XAML代码如下:

<UserControl x:Class="CustomWpfRouteEvent.RouteEventControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Button Height="30" Width="100" Content="调用路由事件" Click="Button_Click"></Button>
    </Grid>
</UserControl>

2、在用户控件的后台代码中创建自定义路由事件,C#代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 CustomWpfRouteEvent
{
    /// <summary>
    /// RouteEventControl.xaml 的交互逻辑
    /// </summary>
    public partial class RouteEventControl : UserControl
    {
        public RouteEventControl()
        {
            InitializeComponent();
        }

        //1、声明并注册路由事件,使用冒泡策略
        public static readonly RoutedEvent MyClientEvent = EventManager.RegisterRoutedEvent("MyClick",
            RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(RouteEventControl));

        //2、通过.NET事件包装路由事件
        public event RoutedEventHandler MyClick
        {
            add
            {
                AddHandler(MyClientEvent, value);
            }
            remove
            {
                RemoveHandler(MyClientEvent, value);
            }
        }

        /// <summary>
        /// 3、使用按钮的单击事件激发路由事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            RoutedEventArgs arg = new RoutedEventArgs();
            arg.RoutedEvent = MyClientEvent;
            RaiseEvent(arg);
        }
    }
}

3、在主界面中引入新创建的用户控件,使用自定义的路由事件MyClick,并为MyClick事件编写调用的方法,XAML代码如下:

<Window x:Class="CustomWpfRouteEvent.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:u="clr-namespace:CustomWpfRouteEvent"
        Title="演示自定义路由事件" Height="350" Width="525" WindowStartupLocation="CenterScreen">
    <Grid>
        <u:RouteEventControl MyClick="RouteEventControl_MyClick"></u:RouteEventControl>
    </Grid>
</Window>

4、RouteEventControl_MyClick方法的后台代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 CustomWpfRouteEvent
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void RouteEventControl_MyClick(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Hello:" + e.Source.ToString());
        }
    }
}

5、运行程序,单击Button按钮,效果如下所示:

到此这篇关于WPF自定义路由事件的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • WPF自定义路由事件的实例教程

    目录 路由事件模型 [分析代码] [自定义路由事件] 总结 路由事件模型 传统的简单事件模型中,在消息激发是将消息通过事件订阅的然后交给事件的相应者,事件的相应者使用事件的处理器来做出相应,这样就存在一个问题,用户控件内部的事件就不能被外界订阅,因为事件的宿主必须能够直接访问到事件的响应者. 路由事件的事件拥有者和事件的相应者之间则没有直接的显式订阅关系,事件的拥有者则只负责激发事件,事件将有谁相应它并不知道,事件的响应者则有事件的监听器,针对事件进行监听,当有此类事件传递至此事件响应者就使用事

  • WPF路由事件中的三种策略介绍

    什么是路由事件 路由事件是具有更强传播能力的事件,它可以在元素树中向上冒泡和向下隧道传播,并且能够沿着传播路径被事件处理程序来处理. 路由事件允许事件在某个元素上被处理,即使这个事件源自于另外一个元素.事件路由允许某个元素的事件由另外一个元素引发. 路由事件是一种可以针对元素树中的多个侦听器而不是仅仅针对引发该事件的对象调用处理程序的事件.路由事件是一个CLR事件. 路由事件与一般事件的区别在于:路由事件是一种用于元素树的事件,当路由事件触发后,它可以向上或向下遍历可视树和逻辑树,他用一种简单而

  • WPF路由事件之逻辑树和可视树遍历

    一.什么是逻辑树 逻辑树就是描述WPF界面元素的实际构成,它是由程序在XAML中所有的UI元素组成.最显著的特点就是由布局控件.或者其他常用的控件组成. <Window x:Class="WpfRouteEvent.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/win

  • 详解WPF中的隧道路由和冒泡路由事件

    WPF中使用路由事件升级了传统应用开发中的事件,在WPF中使用路由事件能更好的处理事件相关的逻辑,我们从这篇开始整理事件的用法和什么是直接路由,什么是冒泡路由,以及什么是隧道路由. 事件最基本的用法 在基于事件驱动的开发中,把代码放在响应注册的事件的处理函数内,比如Click事件.MouseDown事件.MouseUp事件等等.每个控件响应自己的注册事件,有很多如果在事件上有相互关联和影响的事件,就要在一个业务逻辑里写比较多的代码.而路由事件主要的优势就是路由事件可以在元素树上进行传递,并且沿着

  • WPF自定义路由事件

    与依赖项属性类似,WPF也为路由事件提供了WPF事件系统这一组成.为一个类型添加一个路由事件的方式与为类型添加依赖项属性的方法类似,添加一个自定义路由事件的步骤: 一.声明路由事件变量并注册 定义只读的静态变量字段RouteEvent类来声明一个变量,然后使用EventManager的RegisterRoutedEvent()方法向事件系统注册路由事件,该方法的签名如下: public static RoutedEvent RegisterRoutedEvent(string name, Rou

  • WPF 自定义雷达图开发实例教程

    自定义雷达图表如下: 1.创建UserControl,名为"RadarChartControl" 前台: <UserControl x:Class="WpfApplication2.RadarChartControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/win

  • js实现自定义路由

    本文实现自定义路由,主要是事件hashchange的使用,然后根据我们的业务需求封装. 首先实现一个router的类,并实例化. function _router(config){ this.config = config ? config : {}; } _router.prototype = { event:function(str,callback){ var events = str.split(' '); for (var i in events) window.addEventLis

  • WPF自定义选择年月控件详解

    本文实例为大家分享了WPF自定义选择年月控件的具体代码,供大家参考,具体内容如下 封装了一个选择年月的控件,XAML代码: <UserControl x:Class="SunCreate.CombatPlatform.Client.DateMonthPicker" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.micr

  • C#实现自定义双击事件

    本文以一个简单实例讲述了C#实现自定义双击事件的方法,分享给大家供大家参考之用.具体方法如下: 主要功能代码如下: public partial class Form1 : Form,IMessageFilter { public Form1() { InitializeComponent(); Application.AddMessageFilter(this); } private int WM_LBUTTONDBLCLK = 0x0203; public bool PreFilterMes

  • bootstrap-treeview自定义双击事件实现方法

    bootstrap-treeview是一款效果非常酷的基于bootstrap的jQuery多级列表树插件.该jQuery插件基于Twitter Bootstrap,以简单和优雅的方式来显示一些继承树结构,如视图树.列表树等等.但是不知为什么这个插件没有自带双击事件. 经过多次测试,使用方法$('#tree').dblclick( function () {})和方法$('#tree').on('dblclick',function(){})都不起作用!百思不得其解.最后救助大神,问题解决了,但是

随机推荐