[英]How to get eventdata in Blazor/Razor
I started with Blazor today while already having some experience in web-dev.我今天开始使用 Blazor,同时已经有一些网络开发经验。 However it seems that isn't enough.
然而,这似乎还不够。 I want to get the eventarguments of an onkeydown event, so I can check for an enter-key-press.
我想获得 onkeydown 事件的 eventarguments,以便我可以检查回车键。
I already tried to use a function in my event to check the keypress in a separate function, and already tried directly inserting something into the onkeydown event but nothing worked.我已经尝试在我的事件中使用一个函数来检查一个单独的函数中的按键,并且已经尝试直接在 onkeydown 事件中插入一些东西,但没有任何效果。
The following is the event I want to get the keypress from.以下是我想从中获取按键的事件。
<input onkeydown="" bind="@todo.Title" />
You need to use UIKeyboardEventArgs
, something like passing event as argument in JavaScript .您需要使用
UIKeyboardEventArgs
,就像在JavaScript UIKeyboardEventArgs
事件作为参数传递一样。
<p id="p" onclick="doSomething(event);">
In Blazor you'll do it in the following way:在 Blazor 中,您将通过以下方式执行此操作:
<input type="text" onkeypress="@(e => KeyWasPressed(e))" />
/* For .NET Core 3.0+ as per: https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.web.keyboardeventargs?view=aspnetcore-3.1 */
@functions {
private void KeyWasPressed(KeyboardEventArgs args)
{
if (args.Key == "r")
{
Console.WriteLine("R was pressed");
}
}
}
/* For old .NET Core versions */
@functions {
private void KeyWasPressed(UIKeyboardEventArgs args)
{
if (args.Key == "r")
{
Console.WriteLine("R was pressed");
}
}
}
As @Bohring already mentioned in comments you'll still get the event arguments if you're writing onkeypress="@KeyWasPressed"
正如@Bohring 在评论中已经提到的那样,如果您正在编写
onkeypress="@KeyWasPressed"
您仍然会得到事件参数
You can read more here about other event args: https://visualstudiomagazine.com/articles/2018/10/01/blazor-event-handling.aspx您可以在此处阅读有关其他事件参数的更多信息: https : //visualstudiomagazine.com/articles/2018/10/01/blazor-event-handling.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.