c# - .Net5 FocusAsync() 未编译

标签 c# blazor .net-5

我是新的 .Net5 框架上的 MS Blazor 新手。我试图在单击按钮时设置文本元素的焦点。我在其他地方看到过这段代码并试图让它编译:

<button @onclick="() => textInput.FocusAsync()">Set focus</button><input @ref="textInput"/>
VS2019 在 textInput 值上抛出错误:当前上下文中不存在名称“textInput”。
任何帮助表示赞赏。

最佳答案

在您的 @code您需要创建一个名为 textInput 的属性/变量的部分类型为 ElementReference .然后在 ElementReference 上有一个扩展方法这是在 Microsoft.AspNetCore.Components名为 FocusAsync 的命名空间您应该可以调用它。

<button @onclick="() => textInput.FocusAsync()">Set focus</button><input @ref="textInput"/>

@code {
   private ElementReference textInput;
}

关于c# - .Net5 FocusAsync() 未编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65692110/

相关文章:

C# Blazor : How to prevent specific key on input like in JS with e. preventDefault()?

c# - Main() 方法仅在请求到来时执行

c# - 空的 ASP.NET Core (.NET 5.0) 项目对针对其执行的每个请求持续使用更多 RAM

c# - .NET5 JsonPatchDocument.ApplyTo 在添加或替换时抛出

c# - Newtonsoft.Json,填充字典失败

c# - 如何在 Visual Studio Code 中添加程序集引用?

components - 如何在两个同级 Blazor 组件之间进行通信?

c# - 改进 SortedDictionary 的访问时间

c# - 注入(inject)异常类

c# - 如何在 ASP.Net Blazor 服务器端使用 jCrop