Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .Net5 FocusAsync()未编译_C#_Blazor_.net 5 - Fatal编程技术网

C# .Net5 FocusAsync()未编译

C# .Net5 FocusAsync()未编译,c#,blazor,.net-5,C#,Blazor,.net 5,我是新的.Net5框架上的Blazor女士的新手。我试图在单击按钮时设置文本元素的焦点。我在其他地方看到过这段代码,并试图将其编译: <button @onclick="() => textInput.FocusAsync()">Set focus</button><input @ref="textInput"/> 设置焦点 VS2019正在textInput值上引发错误:“textInput”名称在当前上下文

我是新的.Net5框架上的Blazor女士的新手。我试图在单击按钮时设置文本元素的焦点。我在其他地方看到过这段代码,并试图将其编译:

<button @onclick="() => textInput.FocusAsync()">Set focus</button><input @ref="textInput"/>
设置焦点
VS2019正在textInput值上引发错误:“textInput”名称在当前上下文中不存在


非常感谢您的帮助。

@code
部分,您需要创建一个名为
textInput
的属性/变量,该属性/变量的类型为
ElementReference
。然后在
ElementReference
上有一个扩展方法,它位于
Microsoft.AspNetCore.Components
命名空间中,名为
FocusAsync
,您应该能够调用它

<button @onclick="() => textInput.FocusAsync()">Set focus</button><input @ref="textInput"/>

@code {
   private ElementReference textInput;
}
设置焦点
@代码{
私有元素引用文本输入;
}

就是这样!非常感谢。