Brief Summary
This video tutorial explains how to handle UI events in Blazor applications. It covers defining event handlers, handling events from web UI elements and custom components, passing content to a button element, and using additional attributes. Key points include:
- Defining event handlers using synchronous or asynchronous methods, Lambda expressions, and code actions.
- Handling events like
onchange
,oninput
,onmouseover
, andonmouseout
on web UI elements. - Creating custom components with events and passing child content using
RenderFragment
. - Using
CaptureUnmatchedValues
to handle additional attributes on custom components.
Intro
The video introduces handling UI events in Blazor components, focusing on how web UI elements trigger events in response to user interactions and how to manage these events using C# code.
Define an event handler
The video explains different ways to define event handlers for events in Blazor. You can define event handlers using synchronous or asynchronous methods. For asynchronous methods, async Task
is used, and await Task.Delay
can simulate asynchronous behavior. Lambda expressions can be used directly within the attribute value for concise event handling. Visual Studio's code actions can generate synchronous or asynchronous event handlers, including an optional event argument providing additional event properties.
Handling events from web UI elements
The video demonstrates handling events from web UI elements, such as input fields and divs. The onchange
event fires when an input loses focus, while the oninput
event fires with every keystroke. Event arguments can be used to access the value of the input, which may require casting to the appropriate type. Blazor automatically rerenders the component after a UI event is handled. The video also shows how to handle onmouseover
and onmouseout
events on a div, changing the text content based on the mouse's position. Expression-bodied methods are used for concise event handlers.
Handling events from custom components
The video explains how to handle events from custom components by creating a MyButton
component with an onclick
event. An EventCallback<MouseEventArgs>
parameter is defined to allow users of the component to register event handlers. The component's internal button click event invokes the onclick
event callback, passing along the MouseEventArgs
. This allows the parent component to handle the button click event raised by the custom component.
Passing content to a button element
The video describes how to pass content to a custom button element using a RenderFragment
parameter named ChildContent
. This allows the component to capture the content between the opening and closing tags of the MyButton
component and render it inside the HTML button. The RenderFragment
captures the chunk of Razor stuff and renders it someplace else.
Additional attributes
The video explains how to handle additional attributes on a custom component using a dictionary of type Dictionary<string, object>
and the CaptureUnmatchedValues
property. By setting CaptureUnmatchedValues
to true on the parameter attribute, any additional attributes specified on the component are captured in the dictionary. The @attributes
directive is then used to render these additional attributes on the HTML button, allowing users to pass along attributes like class
for styling.