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

    效果展示:


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

     示例下载:http://files.cnblogs.com/BABLOVE/%E7%AE%80%E5%8D%95%E8%87%AA%E5%AE%9A%E4%B9%89%E8%B7%AF%E7%94%B1%E4%BA%8B%E4%BB%B6.rar

  • 相关阅读:
    基于WINCE6.0+S3C2443的camera驱动
    WINCE下基于SL031的RFID功能的实现
    WINCE6.0+S3C2443下的activesync问题
    WINCE6.0+S3C2443下WatchDog学习
    错误的抉择,痛悔
    WINCE5.0下SQL server compact版本更新
    做生意和打工的区别
    WINCE基于MC8630的3G模块的控制和加载
    Xml Tips
    VB.NET and C# Comparison
  • 原文地址:https://www.cnblogs.com/BABLOVE/p/3234311.html
Copyright © 2011-2022 走看看