Improve readability of Blazor Event Handling doc#21755
Conversation
|
@mkArtakMSFT ... Internal Review Topic (links to the Custom event arguments section) If that link doesn't work because it won't open the 6.0 version of the topic, scroll the diff to Line 137. |
|
@mkArtakMSFT ... I'm going to go ahead with this initial coverage to clear this one through. It has a lot of other good UE updates on it. I'll paste the coverage in here for a quick scan. If you spot anything that you don't like, I'm 👂 and will react with a patch PR. ::: moniker range=">= aspnetcore-6.0" Custom event argumentsBlazor supports custom event arguments, which enable you to pass arbitrary data to .NET event handlers with custom events. General configurationCustom events with custom event arguments are generally enabled with the following steps.
Whenever the custom event is fired on the DOM, the event handler is called with the data passed from the Javascript. If you're attempting to fire a custom event, Custom clipboard paste event exampleThe following example receives a custom clipboard paste event that includes the time of the paste and the user's pasted text. Declare a custom name (
[EventHandler("oncustompaste", typeof(CustomPasteEventArgs),
enableStopPropagation: true, enablePreventDefault: true)]
public static class EventHandlers
{
}
public class CustomPasteEventArgs : EventArgs
{
public DateTime EventTimestamp { get; set; }
public string PastedData { get; set; }
}Add JavaScript code to supply data for the xref:System.EventArgs subclass. In the
<script>
Blazor.registerCustomEventType('custompaste', {
browserEventName: 'paste',
createEventArgs: event => {
return {
eventTimestamp: new Date(),
pastedData: event.clipboardData.getData('text')
};
}
});
</script>The preceding code tells the browser that when a native
Event name conventions differ between .NET and JavaScript:
In a Razor component, attach the custom handler to an element.
@page "/custom-paste-arguments"
<label>
Try pasting into the following text box:
<input @oncustompaste="HandleCustomPaste" />
</label>
<p>
@message
</p>
@code {
private string message;
private void HandleCustomPaste(CustomPasteEventArgs eventArgs)
{
message = $"At {eventArgs.EventTimestamp.ToShortTimeString()}, " +
$"you pasted: {eventArgs.PastedData}";
}
}::: moniker-end |
Addresses #19286
Fixes #21763
Internal Review Topic