我是新的 .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/