如何在我的组件中拥有多个 InvokeAsync class

how to have more than one InvokeAsync in my component class

是否可以在我的组件中放置多个 InvokeAsync? 因为我的组件有时会返回一些小东西,如果我创建不同的文件并且 类 每个文件都很难管理它们

public async Task<IViewComponentResult> InvokeAsync(
    int maxPriority, bool isDone)
{
    string MyView = "Default";
    // If asking for all completed tasks, render with the "PVC" view.
    if (maxPriority > 3 && isDone == true)
    {
        MyView = "PVC";
    }
    var items = await GetItemsAsync(maxPriority, isDone);
    return View(MyView, items);
}

public async Task<IViewComponentResult> myInvokeAsync2(
    int maxPriority, bool isDone)
{
    string MyView = "Default";
    // If asking for all completed tasks, render with the "PVC" view.
    if (maxPriority > 3 && isDone == true)
    {
        MyView = "PVC";
    }
    var items = await GetItemsAsync(maxPriority, isDone);
    return View(MyView, items);
}

如何称呼它?

@await Component.InvokeAsync("PriorityList", new { maxPriority = 4, isDone = true })
@await Component.myInvokeAsync2("PriorityList", new { maxPriority = 4, isDone = true })

不行,一个组件只能有一个InvokeAsync方法:
View components in ASP.NET Core | Microsoft Docs

但是,如果您要调用的所有方法都采用同一组参数,您可以传入一个额外的参数来指定要调用的方法。

public Task<IViewComponentResult> InvokeAsync(
    int method, int maxPriority, bool isDone) => method switch
{
    2 => Method2(maxPriority, isDone),
    _ => Method1(maxPriority, isDone),
}

private async Task<IViewComponentResult> Method1(
    int maxPriority, bool isDone)
{
    ...
}

private async Task<IViewComponentResult> Method2(
    int maxPriority, bool isDone)
{
    ...
}
@await Component.InvokeAsync("PriorityList", new { method = 1, maxPriority = 4, isDone = true })
@await Component.InvokeAsync("PriorityList", new { method = 2, maxPriority = 4, isDone = true })