diff --git a/aspnetcore/blazor/components/event-handling.md b/aspnetcore/blazor/components/event-handling.md index ddac0b2c41e0..329bbc87e638 100644 --- a/aspnetcore/blazor/components/event-handling.md +++ b/aspnetcore/blazor/components/event-handling.md @@ -97,22 +97,30 @@ Custom events with custom event arguments are generally enabled with the followi } ``` -1. Register the custom event with the preceding handler in `wwwroot/index.html` (Blazor WebAssembly) or `Pages/_Host.cshtml` (Blazor Server) immediately after the Blazor ` + } ``` + In the preceding example, the `{PACKAGE ID/ASSEMBLY NAME}` placeholder of the file name represents the package ID or assembly name of the app. + > [!NOTE] > The call to `registerCustomEventType` is performed in a script only once per event. + > + > For the call to `registerCustomEventType`, use the `blazor` parameter (lowercase `b`) provided by `afterStarted`. Although the registration is valid when using the `Blazor` object (uppercase `B`), the preferred approach is to use the parameter. 1. Define a class for the event arguments: ```csharp + namespace BlazorSample.CustomEvents; + public class CustomEventArgs : EventArgs { public string? CustomProperty1 {get; set;} @@ -120,9 +128,20 @@ Custom events with custom event arguments are generally enabled with the followi } ``` -1. Wire up the custom event with the event arguments by adding an attribute annotation for the custom event. The class doesn't require members. Note that the class *must* be called `EventHandlers` in order to be found by the Razor compiler, but you should put it in a namespace specific to your app: +1. Wire up the custom event with the event arguments by adding an attribute annotation for the custom event: + + * In order for the compiler to find the `[EventHandler]` class, it must be placed into a C# class file (`.cs`), making it a normal top-level class. + * Mark the class `public`. + * The class doesn't require members. + * The class *must* be called "`EventHandlers`" in order to be found by the Razor compiler. + * Place the class under a namespace specific to your app. + * Import the namespace into the Razor component (`.razor`) where the event is used. ```csharp + using Microsoft.AspNetCore.Components; + + namespace BlazorSample.CustomEvents; + [EventHandler("oncustomevent", typeof(CustomEventArgs), enableStopPropagation: true, enablePreventDefault: true)] public static class EventHandlers { @@ -132,6 +151,8 @@ Custom events with custom event arguments are generally enabled with the followi 1. Register the event handler on one or more HTML elements. Access the data that was passed in from JavaScript in the delegate handler method: ```razor + @using namespace BlazorSample.CustomEvents + @code @@ -159,6 +180,10 @@ Declare a custom name (`oncustompaste`) for the event and a .NET class (`CustomP `CustomEvents.cs`: ```csharp +using Microsoft.AspNetCore.Components; + +namespace BlazorSample.CustomEvents; + [EventHandler("oncustompaste", typeof(CustomPasteEventArgs), enableStopPropagation: true, enablePreventDefault: true)] public static class EventHandlers @@ -172,24 +197,29 @@ public class CustomPasteEventArgs : EventArgs } ``` -Add JavaScript code to supply data for the subclass. In the `wwwroot/index.html` or `Pages/_Host.cshtml` file, add the following ` +} ``` +In the preceding example, the `{PACKAGE ID/ASSEMBLY NAME}` placeholder of the file name represents the package ID or assembly name of the app. + +> [!NOTE] +> For the call to `registerCustomEventType`, use the `blazor` parameter (lowercase `b`) provided by `afterStarted`. Although the registration is valid when using the `Blazor` object (uppercase `B`), the preferred approach is to use the parameter. + The preceding code tells the browser that when a native [`paste`](https://developer.mozilla.org/docs/Web/API/Element/paste_event) event occurs: * Raise a `custompaste` event. @@ -208,6 +238,7 @@ In a Razor component, attach the custom handler to an element. ```razor @page "/custom-paste-arguments" +@using BlazorSample.CustomEvents