WPF:Events事件

哑域析取
• 阅读 4132

AddingEventHandler添加事件句柄

WPF:Events事件

其原理的就是在处理初始button元素的点击事件方法时,同时委托方法到新建项button的点击事件上。

private void MakeButton(object sender, RoutedEventArgs e)
{
    var b2 = new Button {Content = "New Button"};
    // Associate event handler to the button. You can remove the event 
    // handler using "-=" syntax rather than "+=".
    b2.Click += Onb2Click;
    root.Children.Insert(root.Children.Count, b2);
    DockPanel.SetDock(b2, Dock.Top);
    text1.Text = "Now click the second button...";
    b1.IsEnabled = false;
}

CustomRoutedEvents自定义路由事件

WPF:Events事件

界面自定义button样式设计及自定义路由事件Tap附加方法

<Window.Resources>
    <Style TargetType="{x:Type local:MyButtonSimple}">
        <Setter Property="Height" Value="20"/>
        <Setter Property="Width" Value="250"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="Background" Value="#808080"/>
    </Style>
</Window.Resources>
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Background="LightGray">
    <local:MyButtonSimple x:Name="mybtnsimple" Tap="TapHandler">Click to see Tap custom event work</local:MyButtonSimple>
</StackPanel>

自定义路由事件Tap:

  1. EventManager.RegisterRoutedEvent()注册路由事件名称Tap,冒泡策略、路由句柄类型RoutedEventHandler,适用本类MyButtonSimple类型
  2. 提供CLR对事件的包装,在CLR事件访问器中add/remove,使用UIElement.AddHandler():为指定的路由事件添加路由事件处理程序,并将该处理程序添加到当前元素的处理程序集合中。
  3. 引发事件,重写OnClick方法,执行引发事件的方法RaiseTapEvent();RaiseTapEvent中初始化路由事件参数Args(TapEvent),使用UIElement.RaiseEvent(e);
public class MyButtonSimple : Button
{
    // Create a custom routed event by first registering a RoutedEventID
    // This event uses the bubbling routing strategy
    public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent(
        "Tap", RoutingStrategy.Bubble, typeof (RoutedEventHandler), typeof (MyButtonSimple));

    // Provide CLR accessors for the event
    public event RoutedEventHandler Tap
    {
        add { AddHandler(TapEvent, value); }
        remove { RemoveHandler(TapEvent, value); }
    }

    // This method raises the Tap event
    private void RaiseTapEvent()
    {
        var newEventArgs = new RoutedEventArgs(TapEvent);
        RaiseEvent(newEventArgs);
    }

    // For demonstration purposes we raise the event when the MyButtonSimple is clicked
    protected override void OnClick()
    {
        RaiseTapEvent();
    }
}

FindingSourceElement寻找事件引发源元素

WPF:Events事件

很简单的使用路由参数e,其Source包含引发事件的对象

private void HandleClick(object sender, RoutedEventArgs e)
{
    // You must cast the sender object as a Button element, or at least as FrameworkElement, to set Width
    var srcButton = e.Source as Button;
    srcButton.Width = 200;
}

ps:OriginalSource: 在父类进行任何可能的 Source 调整之前,获取原始报告源(由纯粹命中测试确定)。

RoutedEventHandling路由事件处理过程

WPF:Events事件

  1. 按钮的点击事件 被放于容器StackPanel中,其为处理事件的处理者send。
  2. e.Source为事件的引发者。
  3. e.OriginalSource为命中测试的元素引发者,比如button里面包含的图像,点击事件的 命中源伟图像,引发源为按钮。如不包含内容,一般命中源和引发者为同一个元素
<StackPanel Name="myStackPanel" HorizontalAlignment="Center" VerticalAlignment="Center" Button.Click="HandleClick">
    <StackPanel.Resources>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Height" Value="20"/>
            <Setter Property="Width" Value="250"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
        </Style>
    </StackPanel.Resources>
    <Button Name="Button1">Item 1</Button>
    <Button Name="Button2">Item 2</Button>
    <TextBlock Name="results"/>
</StackPanel>

扩展:
如何:实现接口事件(C# 编程指南)

  1. 接口可声明 事件。在类中实现接口事件:在类中声明事件,然后在适当的区域调用该事件。
  2. 不常见情况:您的类是从两个以上的接口继承的,每个接口都含有同名事件)。 在这种情况下,您至少要为其中一个事件提供显式接口实现。 为事件编写显式接口实现时,必须编写 add 和 remove 事件访问器。 这两个事件访问器通常由编译器提供,但在这种情况下编译器不能提供。
  3. 您可以提供自己的访问器,以便指定这两个事件是由您的类中的同一事件表示,还是由不同事件表示。 例如,根据接口规范,如果事件应在不同时间引发,则可以将每个事件与类中的一个单独实现关联(实际就是在同一个方法中,前后顺序引发不同的两个事件)。

RoutedEventArgs 类:包含与路由事件相关的状态信息和事件数据。

  1. 可以为单个 RoutedEvent 使用不同的 RoutedEventArgs。
  2. 此类负责打包 RoutedEvent 的事件数据和提供额外的事件状态信息,并且,事件系统将使用此类来调用与路由事件关联的处理程序。
点赞
收藏
评论区
推荐文章
Dax Dax
4年前
jQuery 的事件绑定和事件委托(事件代理)
简单以jQuery的on()方法为例说明:API:1.on(events,selector,data,handler) 如果on()方法的selector参数为空,事件处理程序就被称为直接绑定。每当在被绑定元素上(如下例中被绑定的document元素,译者注)发生事件时,无论这个事件发生在这个元素上还是从内层元素经冒泡而
达里尔 达里尔
3年前
用css的方式取消div的鼠标点击事件
有时候一些场景需要禁止对div等元素进行点击事件,这个时候如果用js的方法会费时费力,代码量还多,这个时候可以用这个css方法来禁止鼠标点击事件css.divpointerevents:auto|none;/这里的属性为none就是不可被点击/
Stella981 Stella981
4年前
RecyclerView 点击事件和长按事件
在Adapter适配器中设置publicvoidonBindViewHolder(Adapter.MyHolderholder,intposition){holder.img.setImageResource(list.get(position).getImg());holder.tv1.setText(list.get(
Stella981 Stella981
4年前
QTreeWidget的Item点击事件
转载:cw123458945(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fblog.csdn.net%2Fcw123458945%2Farticle%2Fdetails%2F8160295)1!/usr/bin/envpython23i
Stella981 Stella981
4年前
Android点击事件
Android点击事件备注全局实现View.OnClickListener或许需要将MainActivity设置为public注册事件btn_login.setOnClickListener(this)btn_logout.setOnClickListen
Stella981 Stella981
4年前
Android的消息循环与Handler机制理解
一、概念1、事件驱动型什么是事件驱动?就是有事了才去处理,没事就躺着不动。假如把用户点击按钮,滑动页面等这些都看作事件,事件产生后程序就执行相应的处理方法,就是属于事件驱动型。2、消息循环把需要处理的事件表示成一个消息,并且把这个消息放入一个队列。消息循环就是一循环,for或者while都一样。从消息队列里面取出未处理的消息,然后调用该消息的
Stella981 Stella981
4年前
React(5)
绑定函数事件在以类继承的方式定义的组件中,为了能方便地调用当前组件的其他成员方法或属性(如:this.state),通常需要将事件处理函数运行时的this指向当前组件实例。run(){  alert('我是一个run方法')}<buttononClick{this.run}执行方法</button //方法
Wesley13 Wesley13
4年前
C#中委托和事件的区别
大致来说,委托是一个类,该类内部维护着一个字段,指向一个方法。事件可以被看作一个委托类型的变量,通过事件注册、取消多个委托或方法。本篇分别通过委托和事件执行多个方法,从中体会两者的区别。□通过委托执行方法classProgram{staticvoidMain(stringargs){
Stella981 Stella981
4年前
ExtJS表单提交时,不用鼠标点击按钮提交,直接通过回车键提交
用ExtJs实现登陆界面,输入用户名和密码,点击确定后登陆。然而人们习惯于在输入两者后按回车键进行登陆。在extjs中,Button本身是支持回车事件的,前提是:焦点在按钮上,此时回车和单击事件是一回事。form中的field事件中有specilkey事件,可以响应回车键。在输入用户名,或者密码后点击"回车",让field响应回车事件,然后通过form提
Stella981 Stella981
4年前
Noark入门之异步事件
引入异步事件主要是为了各模块的解耦,每当完成一个动作时,向系统发布一个事件,由关心的模块自己监听处理,可选择同步处理,异步处理,延迟处理。何时发布事件,当其他模块关心此动作时<br比如获得道具时,任务系统模块要判定完成进度,BI模块需要上报等等都可以监听此事件,已达模块解耦0x00事件源一个实现xyz.noark.core.event