简体   繁体   English

Blazor Wasm - 无法从“方法组”转换为“事件回调”

[英]Blazor Wasm - cannot convert from 'method group' to 'EventCallback'

I have a simple Blazor Server app that works ok.我有一个可以正常工作的简单 Blazor 服务器应用程序。

I'm trying to create an equivalent app using Blazor WebAssembly with dotnet 5.我正在尝试使用 Blazor WebAssembly 和 dotnet 5 创建一个等效的应用程序。

In the working app, I have a razor component that enables the user to upload files using the BlazorFileUploader nuget package在工作应用程序中,我有一个 razor 组件,它使用户能够使用BlazorFileUploader nuget package 上传文件

Markup:标记:

<InputFile multiple OnChange="HandleFileSelected"/>

Code behind:后面的代码:

public async Task HandleFileSelected(IFileListEntry[] files)
        {
            if (!ValidateFiles(files, out _errorMsg))
            {
                return;
            }
etc...

When I try to compile the wasm app, I get the following error当我尝试编译 wasm 应用程序时,出现以下错误

CS1503 Argument 2: cannot convert from 'method group' to 'EventCallback' CS1503 参数 2:无法从“方法组”转换为“事件回调”

错误图像

Any ideas what I'm doing wrong?任何想法我做错了什么?

I dont think the OnChange eventcallback supports a parameter of type IFileListEntry[]我不认为OnChange事件回调支持IFileListEntry[]类型的参数

Try to change your code behind to:尝试将您的代码更改为:

public async Task HandleFileSelected(InputFileChangeEventArgs e)
{
    var files = e.GetMultipleFiles();
}

Jonas is correct;乔纳斯是正确的; your method needs to accept InputFileChangeEventArgs instead of IFileListEntry[] .您的方法需要接受InputFileChangeEventArgs而不是IFileListEntry[] If you hover over OnChange in Visual Studio 2019, you should see something like this:如果您在 Visual Studio 2019 中通过OnChange使用 hover,您应该会看到如下内容: 在此处输入图像描述

The event handler expects a delegate EventCallback that can accept up to 1 parameter of type InputFileChangeEventArgs .事件处理程序需要一个delegate EventCallback ,它最多可以接受1InputFileChangeEventArgs类型的参数。 Think of a delegate as a method interface: your declaration of IFileListEntry[] does not match the interface, so you are getting an error.delegate视为方法接口:您的IFileListEntry[]声明与接口不匹配,因此您收到错误消息。

If you inspect InputFileChangeEventArgs , you can see how to get the files:如果您检查InputFileChangeEventArgs ,您可以看到如何获取文件:

public async Task HandleFileSelected(InputFileChangeEventArgs args)
{
    IReadOnlyList<Microsoft.AspNetCore.Components.Forms.IBrowserFile> files =
        args.GetMultipleFiles(args.FileCount);
    // ...

Thanks to Connor and Jonas for the answers.感谢康纳和乔纳斯的回答。 The cause of the problem was that the old Blazor Server app from was built using.Net Core 3.1.问题的原因是旧的 Blazor 服务器应用程序是使用 .Net Core 3.1 构建的。 This does not contain a "InputFile" component in the Microsoft.AspNetCore.Components.Forms Namespace.这不包含 Microsoft.AspNetCore.Components.Forms 命名空间中的“InputFile”组件。 This explains why I'd resorted to the 3rd party "BlazorFileUploader" nuget package.这就解释了为什么我求助于第三方“BlazorFileUploader”nuget package。

When building the new Blazor WebAssembly app, I'd chosen dotnet 5. The problem was, the markup was now resolving "InputFile" to the Microsoft.AspNetCore.Components.Forms Namespace rather than the BlazorFileUploader namespace.在构建新的 Blazor WebAssembly 应用程序时,我选择了 dotnet 5。问题是,标记现在将“InputFile”解析为 Microsoft.AspNetCore.Components.Forms 命名空间而不是 BlazorFileUploader 命名空间。

So, I could fix in one of two ways:因此,我可以通过以下两种方式之一进行修复:

  • Fully qualify the markup to <BlazorFileUploader.InputFile将标记完全限定为 <BlazorFileUploader.InputFile
  • Remove the 3rd party nuget package and refactor the code to make use of the InputFile component now available in the dotnet5 version of the Microsoft namespace删除第 3 方 nuget package 并重构代码以使用 Microsoft 命名空间的 dotnet5 版本中现在可用的 InputFile 组件

I decided to go with the seconds of the two options.我决定 go 用秒的两个选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从方法组转换为事件回调 blazor(服务器应用程序)-同步融合网格 - cannot convert from method group to eventcallback blazor (Server App) - Sync Fusion Grid C# Blazor WebAssembly:参数 2:无法从“void”转换为“Microsoft.AspNetCore.Components.EventCallback” - C# Blazor WebAssembly: Argument 2: cannot convert from 'void' to 'Microsoft.AspNetCore.Components.EventCallback' 如何在 Blazor WASM 中向多个侦听器广播 EventCallback? - How do I broadcast an EventCallback to multiple listeners in Blazor WASM? Blazor 从子组件传递并运行任务 - 无法从“方法组”转换为“任务”<int> '</int> - Blazor pass and run Task from child component - cannot convert from 'method group' to 'Task<int>' 无法调试 Blazor wasm - Cannot debug Blazor wasm 如何从 blazor 中的 EventCallback 获取返回值? - How to get return value from EventCallback in blazor? Blazor WASM 应用程序在处理执行删除请求的模态 ok 事件回调后停止响应 - Blazor WASM application stops responding after handling modal ok eventcallback, which performs a delete request 无法从方法组转换为 ushort - cannot convert from method group to ushort 无法从“方法组”转换为“字符串” - cannot convert from "method group" to "string" 参数2:无法从方法组转换为InfoItem - Argument 2: cannot convert from method group to InfoItem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM