zoukankan      html  css  js  c++  java
  • WPF自定义路由事件(一)

    首先自定义事件支持事件路由,需要使用 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);
            }
    
        }
    }
    复制代码

     

    效果展示:

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

  • 相关阅读:
    HDU 4801 Pocket Cube
    HDU 5008 Boring String Problem(后缀数组+二分)
    2-Sat问题
    后缀数组
    树形DP
    图论
    SRM 628 DIV2
    组合博弈
    Github使用笔记
    VS2010+OpenCV配置
  • 原文地址:https://www.cnblogs.com/fuchongjundream/p/3894791.html
Copyright © 2011-2022 走看看