Blazor的FocusAsync不总是设置焦点
我无法制作.NET 6的大型异步方法。
我可以告诉我,异步仅在组件值不为null时起作用。该模型确保组件值不是零。
模型:
namespace BlazorAppWithService.Models;
public class TagModel
{
public string? TagName { get; set; } = "";
}
Blazor标记:
<EditForm Model="@newTag" OnValidSubmit="AddTag">
<InputText @ref="ele "@bind-Value="newTag.TagName"></InputText>
<button type="submit">Add tag</button>
</EditForm>
此代码:
@code {
InputText ele;
private List<TagModel> tagList = new List<TagModel>();
private TagModel newTag = new TagModel();
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
if (ele.Element != null)
await ele.Element.Value.FocusAsync();
}
private async void AddTag()
{
tagList.Add(newTag);
newTag = new TagModel();
if (ele.Element != null)
{
await ele.Element.Value.FocusAsync();
}
}
}
此代码正确将重点放在第一个渲染上,但是当调用AddTag()方法时,组件并未集中。
FWIW,这也不能定位光标:
<button @onclick="() => ele.Element.Value.FocusAsync()" type="submit">Add tag</button>
大火:使困难的事情变得容易,而且很难!
I can't make .NET 6's Blazor AsyncFocus method work.
Near as I can tell, AsyncFocus only works when a component value isn't null. The model ensures the component value isn't null.
Model:
namespace BlazorAppWithService.Models;
public class TagModel
{
public string? TagName { get; set; } = "";
}
Blazor markup:
<EditForm Model="@newTag" OnValidSubmit="AddTag">
<InputText @ref="ele "@bind-Value="newTag.TagName"></InputText>
<button type="submit">Add tag</button>
</EditForm>
This code:
@code {
InputText ele;
private List<TagModel> tagList = new List<TagModel>();
private TagModel newTag = new TagModel();
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
if (ele.Element != null)
await ele.Element.Value.FocusAsync();
}
private async void AddTag()
{
tagList.Add(newTag);
newTag = new TagModel();
if (ele.Element != null)
{
await ele.Element.Value.FocusAsync();
}
}
}
This code correctly sets focus on the first render, but the component is not focused when the AddTag() method is called.
FWIW, this doesn't position cursor either:
<button @onclick="() => ele.Element.Value.FocusAsync()" type="submit">Add tag</button>
Blazor: Making the hard things easy and the easy things hard!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要解释引擎盖下的
focusAsync 正在调用javaScript,但是在这种情况发生后,Blazor正在重新启动。因此,您需要在类似的onFterRenderAsync方法中调用
focusAsync
:focustAsync
使用JavaScript并不是很明显,但这就是为什么它没有的原因工作。文档中有有关调用JavaScript的文档的摘录:source
To explain what is going on
FocusAsync
under the hood is calling JavaScript, but Blazor is rerendering after this happens. Therefore, you need to callFocusAsync
in the OnAfterRenderAsync method like so:Probably it isn't very apparent that
FocusAsync
uses JavaScript, but this is the reason why it doesn't work. There is this excerpt from the documentation about calling JavaScript:source
对我来说,问题是目标要素不在那里。想象一下这种情况:
因此,我们始终以某种方式呼叫
focustAsync
完全呈现元素。注意Editform
之类的容器组件可能会影响其孩子的渲染行为。 (我认为这是您问题的根源。)For me, the problem was the target element not being there. Imagine this scenario:
So we should always somehow call
FocusAsync
after rendering the element completely. Beware that container components likeEditForm
can affect the rendering behavior of their children. (Which I believe is the source of your problem.)