小编Sou*_*uke的帖子

ElementReference 对 Blazor 中条件创建的元素的引用

我正在尝试将焦点设置为有条件呈现的输入控件。我正在设置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 尚未正确配置

完整错误消息:

在此输入图像描述

出现错误的工作示例https://blazorrepl.com/repl/wbueaMPK28hf2NNv09

c# asp.net blazor blazor-webassembly

6
推荐指数
1
解决办法
4185
查看次数

标签 统计

asp.net ×1

blazor ×1

blazor-webassembly ×1

c# ×1