我正在尝试将焦点设置为有条件呈现的输入控件。我正在设置ElementReference,但它的 id 和 context 都为空。
<button @onclick="ToggleInput">Show input</button>
@if(showInput) {
<input @ref="inputRef" type="text"/>
}
@code {
private ElementReference inputRef;
private bool showInput;
async void ToggleInput() {
showInput = !showInput;
await inputRef.FocusAsync();
}
}
Run Code Online (Sandbox Code Playgroud)
当我按下按钮时,它在控制台中显示此错误:
System.InvalidOperationException:ElementReference 尚未正确配置
完整错误消息: