在InvokeAsync方法中,`ElementReference` 无法赋值给参数类型 'object?[]?'

后端开发 2026-07-10

我遇到一个错误

Argument type 'Microsoft.AspNetCore.Components.ElementReference' is not assignable to parameter type 'object?[]?'

位于 ```cs protected Microsoft.AspNetCore.Components.ElementReference rootElement;

public async System.Threading.Tasks.ValueTask getRootElementOffsetCoordinates() { return await this.javaScriptRuntime.InvokeAsync( "getDOM_ElementOffsetCoordinates", this.rootElement ); } `` 的第this.rootElement` 行

protected Microsoft.AspNetCore.Components.ElementReference rootElement;


public async System.Threading.Tasks.ValueTask<IValidatableControl.RootElementOffsetCoordinates> getRootElementOffsetCoordinates()
{
  return await this.javaScriptRuntime.InvokeAsync<RootElementOffsetCoordinates>(
    "getDOM_ElementOffsetCoordinates",
    this.rootElement
  );
}

其中 RootElementOffsetCoordinates

record RootElementOffsetCoordinates
{
  public required double Top { get; init; }
  public required double Left { get; init; }
}

据我所知,基本上把 Microsoft.AspNetCore.Components.ElementReference 作为 InvokeAsync 的第二个参数传入是没问题的。我是不是把什么地方弄错了?

解决方案

因为这段代码在调用 IJSRuntime.InvokeAsync 方法,所以它的第二个参数不是可变参数(params)。

如果你想调用 JSRuntimeExtensions.InvokeAsync 的扩展方法,别忘了在文件开头添加using指令:

using Microsoft.JSInterop;
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章