首页 > 其他分享 >Blazor笔记-Component EventCallback

Blazor笔记-Component EventCallback

时间:2024-03-07 15:33:17浏览次数:22  
标签:EventCallback Component EventArgs HTML Media Blazor Other event

更新记录

注意:非教程。纯笔记,日常查询用的。需要教程的小伙伴找几本书看看即可哈哈,有Vue基础的话非常快,概念都是通的。非工作需要不建议深入学习Blazor,深入Vue吧,用的多,哈哈。

完整目录地址:https://www.cnblogs.com/cqpanda/p/17596348.html

点击查看
2024年3月7日 发布。
2023年8月1日 迁移笔记到博客。

Component EventCallback

EventCallback is a delegate-type used to expose events across components

use EventCallback<T> Type define Component Event Property

Cautions:
EventCallback<T> is a struct type.it don’t perform a null check.
EventCallback is asynchronous and can be awaited.

//Define event
[Parameter]
public EventCallback<int> CurrentCountChanged { get; set; }

//Call event
await CurrentCountChanged.InvokeAsync(CurrentCount);

//bind event
<component @bind-CurrentCount:event="事件名称" ></component>

if you require multiple listeners, you can use Action<T>. Otherwise, it would be best if you used EventCallback<T>

修改默认的HTML DOM事件
To change the default event bindings to make use of the oninput event,
we’ll use the @bind:event directive and specify the event name “oninput”.

<input type="number" @bind:event="oninput" />

或者

<input type="number" @bind-value:event="oninput" />

Blazor event and HTML event

When interacting with the DOM, you will need to handle events of an element. Although Blazor events and HTML events share the same name, they are different. The following image illustrates the difference between Blazor events and HTML events:

image

For example, a button with an HTML event is coded as follows:

<button type="button" onclick="alert('HTML event fired!')">HTML Event</button>

On the other hand, a button with a Blazor event is coded as follows:

<button type="button" @onclick='_ => ExampleString = "Hello Blazor School!"'>Blazor Event</button>

@code {
    public string ExampleString { get; set; } = "";
}

Handle HTML event with Blazor

Handling HTML events with Blazor is a common task in web development. Each HTML event comes with its own data, and you can pass this event data to a C# method for further processing. Follow these steps to pass HTML event data to a C# method:

Create a C# method that corresponds to the relevant C# event, such as FocusEventArgs for the onblur event.

public void OnElementBlur(FocusEventArgs args)
{
}

Pass the HTML event data using the @on syntax. For example, to pass focus event data to a OnElementBlur method:

<div @onblur="OnElementBlur"></div>

Here's a table of the HTML events and their corresponding C# events:

HTML Event	C# Event	Event Group
onfocus		FocusEventArgs	Focus
onblur		FocusEventArgs	Focus
onfocusin	FocusEventArgs	Focus
onfocusout	FocusEventArgs	Focus
onmouseover	MouseEventArgs	Mouse
onmouseout	MouseEventArgs	Mouse
onmousemove	MouseEventArgs	Mouse
onmousedown	MouseEventArgs	Mouse
onmouseup	MouseEventArgs	Mouse
onclick		MouseEventArgs	Mouse
ondblclick	MouseEventArgs	Mouse
onwheel		WheelEventArgs	Mouse
onmousewheel	WheelEventArgs	Mouse
oncontextmenu	MouseEventArgs	Mouse
ondrag		DragEventArgs	Drag
ondragend	DragEventArgs	Drag
ondragenter	DragEventArgs	Drag
ondragleave	DragEventArgs	Drag
ondragover	DragEventArgs	Drag
ondragstart	DragEventArgs	Drag
ondrop		DragEventArgs	Drag
onkeydown	KeyboardEventArgs	Keyboard
onkeyup		KeyboardEventArgs	Keyboard
onkeypress	KeyboardEventArgs	Keyboard
onchange	ChangeEventArgs	Input
oninput		ChangeEventArgs	Input
oninvalid	EventArgs	Input
onreset		EventArgs	Input
onselect	EventArgs	Input
onselectstart	EventArgs	Input
onselectionchange	EventArgs	Input
onsubmit	EventArgs	Input
onbeforecopy	EventArgs	Clipboard
onbeforecut	EventArgs	Clipboard
onbeforepaste	EventArgs	Clipboard
oncopy	ClipboardEventArgs	Clipboard
oncut	ClipboardEventArgs	Clipboard
onpaste	ClipboardEventArgs	Clipboard
ontouchcancel	TouchEventArgs	Touch
ontouchend	TouchEventArgs	Touch
ontouchmove	TouchEventArgs	Touch
ontouchstart	TouchEventArgs	Touch
ontouchenter	TouchEventArgs	Touch
ontouchleave	TouchEventArgs	Touch
ongotpointercapture	PointerEventArgs	Pointer
onlostpointercapture	PointerEventArgs	Pointer
onpointercancel	PointerEventArgs	Pointer
onpointerdown	PointerEventArgs	Pointer
onpointerenter	PointerEventArgs	Pointer
onpointerleave	PointerEventArgs	Pointer
onpointermove	PointerEventArgs	Pointer
onpointerout	PointerEventArgs	Pointer
onpointerover	PointerEventArgs	Pointer
onpointerup	PointerEventArgs	Pointer
oncanplay	EventArgs	Media
oncanplaythrough	EventArgs	Media
oncuechange	EventArgs	Media
ondurationchange	EventArgs	Media
onemptied	EventArgs	Media
onpause	EventArgs	Media
onplay	EventArgs	Media
onplaying	EventArgs	Media
onratechange	EventArgs	Media
onseeked	EventArgs	Media
onseeking	EventArgs	Media
onstalled	EventArgs	Media
onstop	EventArgs	Media
onsuspend	EventArgs	Media
ontimeupdate	EventArgs	Media
onvolumechange	EventArgs	Media
onwaiting	EventArgs	Media
onloadstart	ProgressEventArgs	Progress
ontimeout	ProgressEventArgs	Progress
onabort	ProgressEventArgs	Progress
onload	ProgressEventArgs	Progress
onloadend	ProgressEventArgs	Progress
onprogress	ProgressEventArgs	Progress
onerror	ErrorEventArgs	Progress
onactivate	EventArgs	Other
onbeforeactivate	EventArgs	Other
onbeforedeactivate	EventArgs	Other
ondeactivate	EventArgs	Other
onended	EventArgs	Other
onfullscreenchange	EventArgs	Other
onfullscreenerror	EventArgs	Other
onloadeddata	EventArgs	Other
onloadedmetadata	EventArgs	Other
onpointerlockchange	EventArgs	Other
onpointerlockerror	EventArgs	Other
onreadystatechange	EventArgs	Other
onscroll	EventArgs	Other
ontoggle	EventArgs	Other

Pass parameters to a C# method from the DOM

To pass parameters to a C# method from the DOM, you can use the following syntax:

<button type="button" @onclick="_ => ExampleMethodWithoutHtmlEvent(ExampleString)">Example parameterized method without HTML event</button>

@code {
    public string ExampleString { get; set; } = "";

    public void ExampleMethodWithoutHtmlEvent(string exampleString)
    {
    }
}

If you need to pass HTML event data along with other parameters, you can modify the syntax as follows:

<button type="button" @onclick="(e) => ExampleMethodWithHtmlEvent(e, ExampleString)">Example parameterized method with HTML event</button>

@code {
    public string ExampleString { get; set; } = "";

    public void ExampleMethodWithHtmlEvent(MouseEventArgs eventArgs, string exampleString)
    {
    }
}

preventDefault in Blazor

By default, events in Blazor trigger their respective browser actions. To cancel these actions, you can use the preventDefault() method in JavaScript. This method stops the default actions of the current event.

<input type="checkbox" @onclick:preventDefault />

stopPropagation in Blazor

<div @onclick:stopPropagation></div>

标签:EventCallback,Component,EventArgs,HTML,Media,Blazor,Other,event
From: https://www.cnblogs.com/cqpanda/p/17596386.html

相关文章

  • Blazor笔记-Component RenderFragment / ChildContent
    更新记录注意:非教程。纯笔记,日常查询用的。需要教程的小伙伴找几本书看看即可哈哈,有Vue基础的话非常快,概念都是通的。非工作需要不建议深入学习Blazor,深入Vue吧,用的多,哈哈。完整目录地址:https://www.cnblogs.com/cqpanda/p/17596348.html点击查看2024年3月7日发布。2023......
  • Vue学习笔记36--VueComponent构造函数
    VueComponent构造函数<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>VueComponent&......
  • Blazor使用QuickGrid
    @usingMicrosoft.AspNetCore.Components.QuickGrid<PageTitle>PromotionGrid</PageTitle><h1>PromotionGridExample</h1><QuickGridItems="@people"><PropertyColumnProperty="@(p=>p.PersonId)"Sor......
  • Blazor下载文件
    法一:@usingSystem.IO@injectIJSRuntimeJS<PageTitle>FileDownload1</PageTitle><h1>FileDownloadExample1</h1><button@onclick="DownloadFileFromStream">DownloadFileFromStream</button>@code{pri......
  • Blazor绑定数字
    <h1>DecimalBindingExample</h1><p><label>Decimalvalue(&plusmn;0.000format):<input@bind="DecimalValue"/></label></p><p><code>decimalValue</code>:......
  • Blazor之onclick事件更新值
    <h1>EventHandlerExample1</h1><h2>@headingValue</h2><p><button@onclick="UpdateHeading">Updateheading</button></p><p><label><inputtype="checkb......
  • Blazor常用事件
    一、Input事件:<h1>BindEventExample</h1><p><label>InputValue:<input@bind="InputValue"@bind:event="oninput"/></label></p><p><code>InputValue</cod......
  • Spring @Configuration 和 @Component 区别
    Spring@Configuration和@Component区别一句话概括就是@Configuration中所有带@Bean注解的方法都会被动态代理,因此调用该方法返回的都是同一个实例。 @Configuration注解:@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Componentpublic@......
  • Ant for Blazor做单个表的增删查改
    AntforBlazor做单个表的增删查改2024年02月27日花了一天时间弄出来了,基本弄好了,vs2022+blazorserver+net8,引用的AntDesign版本是0.17.4代码里的model和repository是用自己牛腩代码生成器生成的东西,sqlsugar的,记得在program里注入就好相关代码:@page"/Student"@usingSys......
  • MAUI Blazor+MASA开发安卓应用学习笔记 - 设置图标和初始屏幕
    上一期已经成功生成了APK能成功安装到手机上了,图标和初始屏幕很难看,接下来着手修改图标和初始屏幕一、修改图标打开项目文件.csproj,找到以下代码<!--AppIcon--><MauiIconInclude="Resources\AppIcon\appicon.svg"ForegroundFile="Resources\AppIcon\appiconfg.svg"Colo......