.Net5 FocusAsync() 未编译
.Net5 FocusAsync() not compiling
我刚开始使用新的 .Net5 框架上的 MS Blazor。我试图在单击按钮时设置文本元素的焦点。我在其他地方看到过这段代码并试图让它编译:
<button @onclick="() => textInput.FocusAsync()">Set focus</button><input @ref="textInput"/>
VS2019 在 textInput 值上抛出错误:名称 'textInput' 在当前上下文中不存在。
感谢任何帮助。
在您的 @code
部分中,您需要创建一个名为 textInput
的 property/variable,类型为 ElementReference
。 ElementReference
上有一个名为 FocusAsync
的命名空间中的 ElementReference
扩展方法,您应该可以调用它。
<button @onclick="() => textInput.FocusAsync()">Set focus</button><input @ref="textInput"/>
@code {
private ElementReference textInput;
}
我刚开始使用新的 .Net5 框架上的 MS Blazor。我试图在单击按钮时设置文本元素的焦点。我在其他地方看到过这段代码并试图让它编译:
<button @onclick="() => textInput.FocusAsync()">Set focus</button><input @ref="textInput"/>
VS2019 在 textInput 值上抛出错误:名称 'textInput' 在当前上下文中不存在。
感谢任何帮助。
在您的 @code
部分中,您需要创建一个名为 textInput
的 property/variable,类型为 ElementReference
。 ElementReference
上有一个名为 FocusAsync
的命名空间中的 ElementReference
扩展方法,您应该可以调用它。
<button @onclick="() => textInput.FocusAsync()">Set focus</button><input @ref="textInput"/>
@code {
private ElementReference textInput;
}