Blazor:ASP.NET Core Blazor 事件处理

2020年5月11日 4439点热度 2人点赞 0条评论
内容纲要

Blazor 事件分类

Blazor 组件中, HTML 元素可以触发事件,使用 @on{事件名} 格式绑定触发的事件。

@on{}是触发形式,指明如何触发。

例如:

    <button @onclick="MyEvent">按钮</button>
    @code{
    private void MyEvent()
    {
    }
}

Blazor 中,HTML 元素的事件和绑定事件相关的方式众多,要记下来不容易,因此笔者对此进行了汇总和整理。

其触发的事件类型如下:

类型 说明
BindAttributes 使用@bind标记元素来绑定事件
ClipboardEventArgs 提供有关正在引发的剪贴板事件的信息。
DataTransfer DataTransfer对象用于保存在拖放操作期间拖放的数据。
DataTransferItem DataTransferItem对象表示一个拖动数据项
DragEventArgs 提供有关正在引发的拖动事件的信息。
ErrorEventArgs 提供有关正在引发的错误事件的信息。
EventHandlers 保存EventHandler属性,以配置事件名称和事件参数类型之间的映射。
FocusEventArgs 提供有关正在引发的焦点事件的信息。
KeyboardEventArgs 提供有关正在引发的键盘事件的信息。
MouseEventArgs 提供有关正在引发的鼠标事件的信息。
PointerEventArgs 提供有关正在引发的指针事件的信息。
ProgressEventArgs 提供有关正在引发的进度事件的信息。
TouchEventArgs 提供有关正在引发的触摸事件的信息。
TouchPoint 表示触摸敏感设备上的单个接触点。
WebEventCallbackFactoryEventArgsExtensions 拓展类,为EventCallbackFactory和EventArgs类型提供扩展方法。
WebRenderTreeBuilderExtensions 拓展类提供用于构建RenderTreeFrame条目集合的方法。
WheelEventArgs 提供有关正在引发的鼠标滚轮事件的信息。

以上类型中,大部分作为事件得参数传递。

事件参数类型

HTML 中,元素可以触发得事件非常多,那么不同的 DOM 事件,Blazor 绑定后,触发事件时对应传递的参数类型如下:

事件 DOM 事件和说明
剪贴板 ClipboardEventArgs oncut, oncopy, onpaste
拖动 DragEventArgs ondrag, ondragstart, ondragenter, ondragleave, ondragover,
ondrop, ondragend DataTransferDataTransferItem 保留拖动的项数据。
错误 ErrorEventArgs onerror
事件 EventArgs 常规onactivateonbeforeactivateonbeforedeactivate
ondeactivateonendedonfullscreenchange
onfullscreenerroronloadeddataonloadedmetadata
onpointerlockchangeonpointerlockerroronreadystatechangeonscroll
剪贴板onbeforecut, onbeforecopy, onbeforepaste
输入oninvalid, onreset, onselect, onselectionchange,
onselectstart, onsubmit 介质 oncanplayoncanplaythrough
oncuechangeondurationchangeonemptiedonpause
onplayonplayingonratechangeonseekedonseeking
onstalledonstoponsuspendontimeupdateonvolumechange
onwaiting
焦点 FocusEventArgs onfocus, onblur, onfocusin, onfocusout 不包含对 relatedTarget 的支持。
输入 ChangeEventArgs onchangeoninput
键盘 KeyboardEventArgs onkeydown, onkeypress, onkeyup
鼠标 MouseEventArgs onclick, oncontextmenu, ondblclick, onmousedown, onmouseup, onmouseover, onmousemove, onmouseout
鼠标指针 PointerEventArgs onpointerdown, onpointerup, onpointercancel, onpointermove, onpointerover, onpointerout, onpointerenter, onpointerleave, ongotpointercapture, onlostpointercapture
鼠标滚轮 WheelEventArgs onwheelonmousewheel
进度 ProgressEventArgs onabort, onload, onloadend, onloadstart, onprogress, ontimeout
触控 TouchEventArgs ontouchstart, ontouchend, ontouchmove, ontouchenter, ontouchleave, ontouchcancel TouchPoint 表示触控敏感型设备上的单个接触点。

Blazor 触发方式

其中,EventHandlers 用与配置事件名称和事件参数类型之间的映射关系,就是指生成 @on{EventName}

以下是其中一部分:

file

上诉内容均为其特性。完整定义如下:

using System;

namespace Microsoft.AspNetCore.Components.Web
{
    // Token: 0x02000009 RID: 9
    [EventHandler("onfocus", typeof(FocusEventArgs), true, true)]
    [EventHandler("onblur", typeof(FocusEventArgs), true, true)]
    [EventHandler("onfocusin", typeof(FocusEventArgs), true, true)]
    [EventHandler("onfocusout", typeof(FocusEventArgs), true, true)]
    [EventHandler("onmouseover", typeof(MouseEventArgs), true, true)]
    [EventHandler("onmouseout", typeof(MouseEventArgs), true, true)]
    [EventHandler("onmousemove", typeof(MouseEventArgs), true, true)]
    [EventHandler("onmousedown", typeof(MouseEventArgs), true, true)]
    [EventHandler("onmouseup", typeof(MouseEventArgs), true, true)]
    [EventHandler("onclick", typeof(MouseEventArgs), true, true)]
    [EventHandler("ondblclick", typeof(MouseEventArgs), true, true)]
    [EventHandler("onwheel", typeof(WheelEventArgs), true, true)]
    [EventHandler("onmousewheel", typeof(WheelEventArgs), true, true)]
    [EventHandler("oncontextmenu", typeof(MouseEventArgs), true, true)]
    [EventHandler("ondrag", typeof(DragEventArgs), true, true)]
    [EventHandler("ondragend", typeof(DragEventArgs), true, true)]
    [EventHandler("ondragenter", typeof(DragEventArgs), true, true)]
    [EventHandler("ondragleave", typeof(DragEventArgs), true, true)]
    [EventHandler("ondragover", typeof(DragEventArgs), true, true)]
    [EventHandler("ondragstart", typeof(DragEventArgs), true, true)]
    [EventHandler("ondrop", typeof(DragEventArgs), true, true)]
    [EventHandler("onkeydown", typeof(KeyboardEventArgs), true, true)]
    [EventHandler("onkeyup", typeof(KeyboardEventArgs), true, true)]
    [EventHandler("onkeypress", typeof(KeyboardEventArgs), true, true)]
    [EventHandler("onchange", typeof(ChangeEventArgs), true, true)]
    [EventHandler("oninput", typeof(ChangeEventArgs), true, true)]
    [EventHandler("oninvalid", typeof(EventArgs), true, true)]
    [EventHandler("onreset", typeof(EventArgs), true, true)]
    [EventHandler("onselect", typeof(EventArgs), true, true)]
    [EventHandler("onselectstart", typeof(EventArgs), true, true)]
    [EventHandler("onselectionchange", typeof(EventArgs), true, true)]
    [EventHandler("onsubmit", typeof(EventArgs), true, true)]
    [EventHandler("onbeforecopy", typeof(EventArgs), true, true)]
    [EventHandler("onbeforecut", typeof(EventArgs), true, true)]
    [EventHandler("onbeforepaste", typeof(EventArgs), true, true)]
    [EventHandler("oncopy", typeof(ClipboardEventArgs), true, true)]
    [EventHandler("oncut", typeof(ClipboardEventArgs), true, true)]
    [EventHandler("onpaste", typeof(ClipboardEventArgs), true, true)]
    [EventHandler("ontouchcancel", typeof(TouchEventArgs), true, true)]
    [EventHandler("ontouchend", typeof(TouchEventArgs), true, true)]
    [EventHandler("ontouchmove", typeof(TouchEventArgs), true, true)]
    [EventHandler("ontouchstart", typeof(TouchEventArgs), true, true)]
    [EventHandler("ontouchenter", typeof(TouchEventArgs), true, true)]
    [EventHandler("ontouchleave", typeof(TouchEventArgs), true, true)]
    [EventHandler("ongotpointercapture", typeof(PointerEventArgs), true, true)]
    [EventHandler("onlostpointercapture", typeof(PointerEventArgs), true, true)]
    [EventHandler("onpointercancel", typeof(PointerEventArgs), true, true)]
    [EventHandler("onpointerdown", typeof(PointerEventArgs), true, true)]
    [EventHandler("onpointerenter", typeof(PointerEventArgs), true, true)]
    [EventHandler("onpointerleave", typeof(PointerEventArgs), true, true)]
    [EventHandler("onpointermove", typeof(PointerEventArgs), true, true)]
    [EventHandler("onpointerout", typeof(PointerEventArgs), true, true)]
    [EventHandler("onpointerover", typeof(PointerEventArgs), true, true)]
    [EventHandler("onpointerup", typeof(PointerEventArgs), true, true)]
    [EventHandler("oncanplay", typeof(EventArgs), true, true)]
    [EventHandler("oncanplaythrough", typeof(EventArgs), true, true)]
    [EventHandler("oncuechange", typeof(EventArgs), true, true)]
    [EventHandler("ondurationchange", typeof(EventArgs), true, true)]
    [EventHandler("onemptied", typeof(EventArgs), true, true)]
    [EventHandler("onpause", typeof(EventArgs), true, true)]
    [EventHandler("onplay", typeof(EventArgs), true, true)]
    [EventHandler("onplaying", typeof(EventArgs), true, true)]
    [EventHandler("onratechange", typeof(EventArgs), true, true)]
    [EventHandler("onseeked", typeof(EventArgs), true, true)]
    [EventHandler("onseeking", typeof(EventArgs), true, true)]
    [EventHandler("onstalled", typeof(EventArgs), true, true)]
    [EventHandler("onstop", typeof(EventArgs), true, true)]
    [EventHandler("onsuspend", typeof(EventArgs), true, true)]
    [EventHandler("ontimeupdate", typeof(EventArgs), true, true)]
    [EventHandler("onvolumechange", typeof(EventArgs), true, true)]
    [EventHandler("onwaiting", typeof(EventArgs), true, true)]
    [EventHandler("onloadstart", typeof(ProgressEventArgs), true, true)]
    [EventHandler("ontimeout", typeof(ProgressEventArgs), true, true)]
    [EventHandler("onabort", typeof(ProgressEventArgs), true, true)]
    [EventHandler("onload", typeof(ProgressEventArgs), true, true)]
    [EventHandler("onloadend", typeof(ProgressEventArgs), true, true)]
    [EventHandler("onprogress", typeof(ProgressEventArgs), true, true)]
    [EventHandler("onerror", typeof(ErrorEventArgs), true, true)]
    [EventHandler("onactivate", typeof(EventArgs), true, true)]
    [EventHandler("onbeforeactivate", typeof(EventArgs), true, true)]
    [EventHandler("onbeforedeactivate", typeof(EventArgs), true, true)]
    [EventHandler("ondeactivate", typeof(EventArgs), true, true)]
    [EventHandler("onended", typeof(EventArgs), true, true)]
    [EventHandler("onfullscreenchange", typeof(EventArgs), true, true)]
    [EventHandler("onfullscreenerror", typeof(EventArgs), true, true)]
    [EventHandler("onloadeddata", typeof(EventArgs), true, true)]
    [EventHandler("onloadedmetadata", typeof(EventArgs), true, true)]
    [EventHandler("onpointerlockchange", typeof(EventArgs), true, true)]
    [EventHandler("onpointerlockerror", typeof(EventArgs), true, true)]
    [EventHandler("onreadystatechange", typeof(EventArgs), true, true)]
    [EventHandler("onscroll", typeof(EventArgs), true, true)]
    public static class EventHandlers
    {
    }
}

HTML标准事件分类

下面的表格列出了可插入 HTML 元素中以定义事件行为的标准事件属性。

  • Window 事件属性 – Window Event Attributes
  • 表单事件 – Form Events
  • 键盘事件 – Keybord Events
  • 鼠标事件 – Mouse Events
  • 媒介事件 – Media Events

具体不同的 HTML 元素具有的事件属性,请参考 w3school https://www.w3school.com.cn/tags/html_ref_eventattributes.asp

以下附录为抄录 w3school 中,关于各种事件的分类和说明。

1)Window 事件属性

window 对象触发的事件。

适用于 标签:

属性 描述
onafterprint script 在打印文档之后运行脚本
onbeforeprint script 在文档打印之前运行脚本
onbeforeonload script 在文档加载之前运行脚本
onblur script 当窗口失去焦点时运行脚本
onerror script 当错误发生时运行脚本
onfocus script 当窗口获得焦点时运行脚本
onhaschange script 当文档改变时运行脚本
onload script 当文档加载时运行脚本
onmessage script 当触发消息时运行脚本
onoffline script 当文档离线时运行脚本
ononline script 当文档上线时运行脚本
onpagehide script 当窗口隐藏时运行脚本
onpageshow script 当窗口可见时运行脚本
onpopstate script 当窗口历史记录改变时运行脚本
onredo script 当文档执行再执行操作(redo)时运行脚本
onresize script 当调整窗口大小时运行脚本
onstorage script 当文档加载加载时运行脚本
onundo script 当 Web Storage 区域更新时(存储空间中的数据发生变化时)
onunload script 当用户离开文档时运行脚本

2)表单事件

由 HTML 表单内部的动作触发的事件。

属性 描述
onblur script 当元素失去焦点时运行脚本
onchange script 当元素改变时运行脚本
oncontextmenu script 当触发上下文菜单时运行脚本
onfocus script 当元素获得焦点时运行脚本
onformchange script 当表单改变时运行脚本
onforminput script 当表单获得用户输入时运行脚本
oninput script 当元素获得用户输入时运行脚本
oninvalid script 当元素无效时运行脚本
onreset script 当表单重置时运行脚本。HTML 5 不支持。
onselect script 当选取元素时运行脚本
onsubmit script 当提交表单时运行脚本

3)键盘事件

由键盘触发的事件。

属性 描述
onkeydown script 当按下按键时运行脚本
onkeypress script 当按下并松开按键时运行脚本
onkeyup script 当松开按键时运行脚本

4)鼠标事件

由鼠标或相似的用户动作触发的事件。

属性 描述
onclick script 当单击鼠标时运行脚本
ondblclick script 当双击鼠标时运行脚本
ondrag script 当拖动元素时运行脚本
ondragend script 当拖动操作结束时运行脚本
ondragenter script 当元素被拖动至有效的拖放目标时运行脚本
ondragleave script 当元素离开有效拖放目标时运行脚本
ondragover script 当元素被拖动至有效拖放目标上方时运行脚本
ondragstart script 当拖动操作开始时运行脚本
ondrop script 当被拖动元素正在被拖放时运行脚本
onmousedown script 当按下鼠标按钮时运行脚本
onmousemove script 当鼠标指针移动时运行脚本
onmouseout script 当鼠标指针移出元素时运行脚本
onmouseover script 当鼠标指针移至元素之上时运行脚本
onmouseup script 当松开鼠标按钮时运行脚本
onmousewheel script 当转动鼠标滚轮时运行脚本
onscroll script 当滚动元素滚动元素的滚动条时运行脚本

5)媒介事件

由视频、图像以及音频等媒介触发的事件。

适用于所有 HTML 5 元素,不过在媒介元素(诸如 audio、embed、img、object 以及 video)中最常用:

属性 描述
onabort script 当发生中止事件时运行脚本
oncanplay script 当媒介能够开始播放但可能因缓冲而需要停止时运行脚本
oncanplaythrough script 当媒介能够无需因缓冲而停止即可播放至结尾时运行脚本
ondurationchange script 当媒介长度改变时运行脚本
onemptied script 当媒介资源元素突然为空时(网络错误、加载错误等)运行脚本
onended script 当媒介已抵达结尾时运行脚本
onerror script 当在元素加载期间发生错误时运行脚本
onloadeddata script 当加载媒介数据时运行脚本
onloadedmetadata script 当媒介元素的持续时间以及其他媒介数据已加载时运行脚本
onloadstart script 当浏览器开始加载媒介数据时运行脚本
onpause script 当媒介数据暂停时运行脚本
onplay script 当媒介数据将要开始播放时运行脚本
onplaying script 当媒介数据已开始播放时运行脚本
onprogress script 当浏览器正在取媒介数据时运行脚本
onratechange script 当媒介数据的播放速率改变时运行脚本
onreadystatechange script 当就绪状态(ready-state)改变时运行脚本
onseeked script 当媒介元素的定位属性 [1] 不再为真且定位已结束时运行脚本
onseeking script 当媒介元素的定位属性为真且定位已开始时运行脚本
onstalled script 当取回媒介数据过程中(延迟)存在错误时运行脚本
onsuspend script 当浏览器已在取媒介数据但在取回整个媒介文件之前停止时运行脚本
ontimeupdate script 当媒介改变其播放位置时运行脚本
onvolumechange script 当媒介改变音量亦或当音量被设置为静音时运行脚本
onwaiting script 当媒介已停止播放但打算继续播放时运行脚本

痴者工良

高级程序员劝退师

文章评论