【发布时间】:2021-07-04 13:50:46
【问题描述】:
我正在尝试将焦点设置为有条件呈现的输入控件。我正在设置 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();
}
}
当我按下按钮时,它会在控制台中显示此错误:
System.InvalidOperationException: ElementReference 配置不正确
完整的错误信息:
【问题讨论】:
-
您可以使用
?将其设置为可选所以await inputRef?.FocusAsync()?或者只是包装一个空检查 -
是的,但是这个参考永远不会被设置,我想知道我必须做些什么才能让它工作。
-
@MiltoxBeyond,
ElementReference永远不会为空,因为它是struct。
标签: c# asp.net blazor blazor-webassembly