如何在单独的 .cs 文件中调用 onkeydown 事件



我想在单独的C# 代码文件中处理onkeydown事件。但是,我总是收到此错误:CS0428无法将方法组"键处理程序"转换为非委托类型"对象"。是否打算调用该方法?

恐怕我错过了正确的语法。目标框架是 .NET Standard 2.0。这是我的文件:

index.razor:

@page "/"
@inherits blazortest.TestBase
<div tabindex="0" @onkeydown=@KeyHandler>
xxx
</div>

测试库.cs:

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
namespace blazortest
{
public class TestBase : LayoutComponentBase
{
public void KeyHandler(KeyboardEventArgs e)
{
// Do something
}
}
}

你的代码应该是:

@page "/"
@inherits blazortest.TestBase
<div tabindex="0" @onkeydown="KeyHandler">
xxx
</div>
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
namespace blazortest
{
public class TestBase : LayoutComponentBase
{
public void KeyHandler(KeyboardEventArgs e)
{
// Do something
}
}
}

最新更新