zoukankan      html  css  js  c++  java
  • WPF事件,路由事件

    直接事件模型或CLR事件模型

    1事件拥有者

    2事件响应者

    3事件订阅关系

    例如 Window窗口中的控件Button

    事件:拥有者Button

    事件:Button.Click

    事件响应者:Window

    事件处理器:Button_click(…..)

    事件订阅 Button.Click+=new system.EventHandle(this.Button_Click)

    CLR事件模型中事件的拥有者也就是消息的发送者(sender)

      private void button_Click(object sender, RoutedEventArgs e)

            {

              

                if(sender is Button)

                {

                    MessageBox.Show((sender as Button).Name);

                }

            }

    缺点:事件拥有者与事件的响应者必须建立订阅这条专线,

    路由事件

    系统路由事件

    事件的拥有者只负责激发事件,事件响应者则安装事件侦听器,当有此类型的事件传递至此时,事件响应者就使用事件处理器来响应事件并决定是否事件可以继续传递
    路由事件是从叶-----------》根传播的

    注册侦听器

    C#

    this.window.AddHandler(Button.ClickEvent, new RoutedEventHandler(this.buutonClick));

    XAML

    <Grid x:Name="gridroot" Margin="10" Button.Click="buutonClick">

    Event

    private void buutonClick(object sender, RoutedEventArgs e)

            {

                MessageBox.Show((e.OriginalSource as Button).Name);

            }

    自定义路由事件

    编写携带参数的事件消息类

    1, public  class ReportTimeEventArgs:RoutedEventArgs

    2,     {

    3,       public ReportTimeEventArgs(RoutedEvent routedEvent, object source) : base(routedEvent, source) { }

    4,       public DateTime ClickTime { get; set; }

    5,      }

    编写路由事件类

      public class TimeButton:Button

        {

           public static readonly RoutedEvent ReprotTimeEvent=

               EventManager.RegisterRoutedEvent("ReprotTime",

       RoutingStrategy.Bubble,typeof(EventHandler<ReportTimeEventArgs>),typeof(TimeButton));

          // CLR事件包装器作用是把事件路由暴露的像一个直接事件一样类似于属行

           public event RoutedEventHandler ReprotTime

           {

               add{ this.AddHandler(ReprotTimeEvent,value);}

               remove{this.RemoveHandler(ReprotTimeEvent,value);}

           }

           protected override void OnClick()

           {

               base.OnClick();

               ReportTimeEventArgs args = new ReportTimeEventArgs(ReprotTimeEvent,this);//准备事消息

               args.ClickTime = DateTime.Now;

               this.RaiseEvent(args);

           }

    }

    注册侦听器

    <Grid local:TimeButton.ReprotTime="ReprotTimeClick" x:Name="gridroot">
    <Grid local:TimeButton.ReprotTime="ReprotTimeClick" x:Name="grid1">
    <Grid local:TimeButton.ReprotTime="ReprotTimeClick" x:Name="grid2">
    <StackPanel local:TimeButton.ReprotTime="ReprotTimeClick" x:Name="stackPanel">
    <ListBox Background="BlueViolet" local:TimeButton.ReprotTime="ReprotTimeClick" x:Name="listBox1">
    <local:TimeButton x:Name="timeButton" Width="80" Height="80" Content="报时" local:TimeButton.ReprotTime="ReprotTimeClick"></local:TimeButton>
    </ListBox>
    </StackPanel>
    </Grid>
    </Grid>

    </Grid>

    RoutingStrategy.Bubble,参数指定WPF路由事件的三种策略

    1, Bubble(冒泡式):从事件的激发着出发向它上级容器一层一层路由,即有Button 到 window

    2         Tunnel(隧道式):与冒泡式相反有Window到Button

    3       3Direct(直达式)直接将消息送达事件处理器

     

  • 相关阅读:
    Matching「CEOI2011」
    中位数之中位数「NOIP多校联考 2019」
    积木「CSP-S全国排位赛第一场」
    序列「CSP-S全国排位赛第一场」
    GT考试「HNOI2008」
    动物园「NOI2014」
    Radio Transmission「BOI2009」
    小Z的袜子「国家集训队」
    BZOJ3624: [Apio2008]免费道路
    BZOJ1190: [HNOI2007]梦幻岛宝珠
  • 原文地址:https://www.cnblogs.com/wangboke/p/5320390.html
Copyright © 2011-2022 走看看