[英].Net5 FocusAsync() not compiling
我是 new.Net5 框架上的新手,正在玩 MS Blazor。 我试图在单击按钮时设置文本元素的焦点。 我在其他地方看到过这段代码并试图让它编译:
<button @onclick="() => textInput.FocusAsync()">Set focus</button><input @ref="textInput"/>
VS2019 在 textInput 值上抛出错误:当前上下文中不存在名称“textInput”。
任何帮助表示赞赏。
在您的@code
部分中,您需要创建一个名为textInput
的属性/变量,其类型为ElementReference
。 然后,在名为FocusAsync
的Microsoft.AspNetCore.Components
命名空间中的ElementReference
上有一个扩展方法,您应该可以调用它。
<button @onclick="() => textInput.FocusAsync()">Set focus</button><input @ref="textInput"/>
@code {
private ElementReference textInput;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.