当用户在文本框 (oninput) 中输入时如何调用异步方法?

How do you call an async method when the user is typing in a textbox (oninput)?

我想在每次对文本框进行更改时调用异步方法。

我已经看了一段时间了,现在正在谷歌搜索并尝试我找到的语法。下面的代码似乎是最准确的。

Razor 页面代码:

    <div class="offset-sm-5 col-sm-3">
        <div class="input-group">
            <input class="form-control" @oninput="@(async () => await OnSearchAsync())"/>
        </div>
    </div>

隐藏代码:

    public class ListBase: ComponentBase
    {
        [Inject]
        public IVpbDelegateAdminService VpbDelegateAdminService { get; set; }

        protected VpbDelegateListVm VpbDelegateListVm { get; set; }

        protected override async Task OnInitializedAsync()
        {
            VpbDelegateListVm  = await VpbDelegateAdminService.GetVpbDelegateListVmAsync(1);
        }

        protected async Task OnSearchAsync()
        {
            VpbDelegateListVm = await VpbDelegateAdminService.GetVpbDelegateListVmAsync(2);
        }
    }

如有任何帮助,我们将不胜感激。

您是否正在尝试从后面的代码中调用方法?

如果是这样,这应该可行。

 <div class="offset-sm-5 col-sm-3">
        <div class="input-group">
            <input class="form-control" @oninput="@OnSearchAsync"/>
        </div>
    </div>

@code
{
    private async Task OnSearchAsync()
    {
        var data = await GetYourData();
    }
}

我的解决方案是

@page "/Demo_domtree_input_typing"

@{ BlazorSession.Current["codename"] = "Demo_domtree_input_typing"; }

Your message :
<BlazorDomTree @ref="dt"></BlazorDomTree>

@code{

    BlazorDomTree dt;

    protected override void OnAfterRender(bool first)
    {
        if (!first)
            return;

        PlusControl inp1 = dt.Root.Create("input type='text'");
        inp1.OnChanging(delegate
        {
            BlazorSession.Current.Toast("Your are typing '" + inp1.Value + "'", 1000, "typinginfo");
        });
    }
}

在线现场演示: http://demo.blazorplus.com/Demo_domtree_input_typing

(需要导入并设置 nuget 包 'BlazorPlus')