You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级MudBlazor至8版本后MudAutocomplete出现CS1666/CS0123错误求助

MudBlazor升级至8版本后CS1666与CS0123编译错误排查

问题背景

原本运行正常的Blazor Razor页面,在将MudBlazor升级到8版本后出现两个编译错误:

  • CS1666:无法在匿名方法、lambda表达式、查询表达式或迭代器中使用包含ref、out或in参数的函数
  • CS0123:没有与委托匹配的重载,参数不匹配

此前代码功能正常,需定位版本更新中遗漏的配置或改动。

我的代码

<EditForm Model="accountrole" OnValidSubmit="OnValidSubmit">
    <DataAnno2tationsValidator />
    <MudDialog>
        <DialogContent>
            @if (accountrole.AccountRoleID > -1)
            {
                <MudAutocomplete T=Account
                Label="Account"
                @bind-Value="selectedAccount"
                SearchFunc=SearchAcc
                Variant="Variant.Outlined"
                Clearable="true"
                ToStringFunc="@(e=> e==null?null : $"{e.AccountName}")" />

                <MudAutocomplete Class="mt-3"
                T=Role
                Label="Account"
                @bind-Value="selectedRole"
                SearchFunc=SearchRole
                Variant="Variant.Outlined"
                Clearable="true"
                ToStringFunc="@(e=> e is null ? null : $"{e.Title}")" />
            }
        </DialogContent>
        <DialogActions>
            <MudButton OnClick="Cancel">Cancel</MudButton>
            <MudButton ButtonType="ButtonType.Submit"
            Variant="Variant.Filled"
            Color="MudBlazor.Color.Primary">
                @(mode == "edit" ? "Update" : "Save")
            </MudButton>
        </DialogActions>
    </MudDialog>
</EditForm>

@code {
    bool success;

    [Parameter] public AccountRole accountrole { get; set; } = new AccountRole();
    [Parameter] public List<Account> accountList { get; set; } = new List<Account>();
    [Parameter] public List<Role> roleList { get; set; } = new List<Role>();
    [Parameter] public string? mode { get; set; }
    [CascadingParameter] IMudDialogInstance? MudDialog { get; set; }

    void Submit() => MudDialog?.Close(DialogResult.Ok(true));
    void Cancel() => MudDialog?.Cancel();

    private async Task OnValidSubmit(EditContext context)
    {
        success = true;
        await addAccountRole();
    }

    // AUTOCOMPLETE FOR Account
    private Account? selectedAccount;
    private async Task<IEnumerable<Account>> SearchAcc(string value)
    {
        StateHasChanged();
        await Task.Delay(5);
        await Task.Yield();
        return (string.IsNullOrEmpty(value)) ? accountList : accountList.Where(x => x.AccountName.Contains(value, StringComparison.InvariantCultureIgnoreCase));
    }

    // AUTOCOMPLETE FOR Role
    private Role? selectedRole;
    private async Task<IEnumerable<Role>> SearchRole(string value)
    {
        StateHasChanged();
        await Task.Delay(5);
        return (string.IsNullOrEmpty(value)) ? roleList : roleList.Where(x => x.Title.Contains(value, StringComparison.InvariantCultureIgnoreCase));
    }
}

错误原因与修复方案

MudBlazor 8对MudAutocomplete组件的SearchFunc参数做了破坏性API变更,同时代码中存在拼写错误,具体修复如下:

1. 适配SearchFunc的新签名

MudBlazor 8将SearchFunc的签名从Func<string, Task<IEnumerable<T>>>更新为Func<string, CancellationToken, Task<IEnumerable<T>>>,新增了CancellationToken参数用于支持取消操作,这是导致CS0123错误的直接原因。

修改两个搜索方法的签名,添加CancellationToken参数,并优化代码:

// 修改Account搜索方法
private async Task<IEnumerable<Account>> SearchAcc(string value, CancellationToken token)
{
    await Task.Delay(5, token); // 使用带token的Delay,支持取消逻辑
    return string.IsNullOrEmpty(value) 
        ? accountList 
        : accountList.Where(x => x.AccountName.Contains(value, StringComparison.InvariantCultureIgnoreCase));
}

// 修改Role搜索方法
private async Task<IEnumerable<Role>> SearchRole(string value, CancellationToken token)
{
    await Task.Delay(5, token);
    return string.IsNullOrEmpty(value) 
        ? roleList 
        : roleList.Where(x => x.Title.Contains(value, StringComparison.InvariantCultureIgnoreCase));
}

Razor页面中绑定SearchFunc时无需手动传参,框架会自动传递CancellationToken,保持原有写法即可。

2. 修正组件拼写错误

代码中DataAnno2tationsValidator存在拼写错误(多了数字2),需修正为正确的DataAnnotationsValidator,否则会导致验证功能失效。

3. 修正逻辑错误(非编译错误)

第二个MudAutocomplete的Label属性写成了"Account",应改为"Role",避免界面混淆。

4. 移除不必要的StateHasChanged调用

SearchFunc方法中手动调用StateHasChanged()是多余的,MudBlazor组件内部会自动处理状态更新,移除后可避免潜在的CS1666错误触发条件。

内容的提问来源于stack exchange,提问作者Remar10

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 04:13:10