'Blazor - cannot convert from 'method group' to 'EventCallback'
I have a parent component and a child component with an event callback where I want it to pass back a type of T.
The issue I'm having is that I have an error about converting
MethodGroup to EventCallback.
If I convert this to using an Action then it works, but I can't do it async which isn't ideal.
Any ideas what I'm doing wrong?
Parent
<Child DeleteCallback="@OnDelete"></Child>
public async Task OnDelete(T item)
{ ... }
Child
@typeparam T
[Parameter]
public EventCallback<T> DeleteCallback { get; set; }
<a @onclick="() => DeleteCallback.InvokeAsync(item)"></a>
I've added a repo here explaining the problem. Looking at the Issues for Blazor, this should;ve been fixed in 2019. https://github.com/scott-david-walker/EventCallbackError
Solution 1:[1]
In my case I declared a nullable EventCallback? - you can't do that.
Solution 2:[2]
I've experienced that the syntax was spot on and still getting this error.
Restarting Visual Studio 2019 solved the problem for me. Cleaning and rebuilding was not enough.
Solution 3:[3]
The following syntax worked for me:
// in the component (C#):
[Parameter]
public EventCallback<MovingEventArgs> OnMoving { get; set; }
// on the using side (Razor):
OnMoving="(Component.MovingEventArgs o) => OnMoving(o)"
// on the using side (C#):
protected void OnMoving( Component.MovingEventArgs e ) {
}
There seems to have been a change and the documentation is not up to date. This event is also using custom event arguments.
Solution 4:[4]
For some reason Visual Studio kept a previous signature I had used. Cleaning, restarting, emptying bin/obj folders etc did not work. I had to rename the method, which worked for me.
Solution 5:[5]
For anyone fighting this with the MudBlazor MudTable I found this worked use @bind-SelectedItem and don't pass any parameters to the commit function.
<MudTable T="POCO" Items="@MyState.POCOs"
@bind-SelectedItem="_selectedPOCO"
OnCommitEditClick="@(() => commitPOCO())"
CommitEditTooltip="Save Changes?">
@code {
private POCO _selectedPOCO;
private async void commitPOCO()
{
// all the changed values are in _selectedPOCO
...
}
Solution 6:[6]
In my case, the problem solved when I defined the @typeparam manually(not by inference)(TItem="int").
<MyComponent TItem="int" OnChange="Component_Changed" />
Solution 7:[7]
Okay I just spent hours trying to figure this out. Turns out on my end the problem was I have sync fusion in the project and I had explicitly define the name space of the Event call back arguments which in my case were Microsoft.AspNetCore.Components.ChangeEventArgs in the child component and then creating an async method to pass back data to the parent.
Solution 8:[8]
In my case it was a phantom error among tens others that never went away no matter what I do. Meaning I could still build and run a solution without problems, despite so many erorrs.
What helped me was this solution: https://stackoverflow.com/a/66219566/1215913
However, I finally found a solution after a few hours of digging. To get rid of the phantom errors I closed Visual Studio, deleted the files in the following folder, and then re-opened the solution: C:\Users<Username>\AppData\Local\Temp\VSFeedbackIntelliCodeLogs\Suggestions\
The only difference is I removed all of the folders that start with VS (C:\Users\<Username>\AppData\Local\Temp\VS*) since there was no this exact path as @rovert mentions in his post.
This seems to be some common problem with Blazor and IntelliSense. I also often see that auto-formatter fails to correctly format the razor code (text indentations are a mess).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sebazzz |
| Solution 2 | JTvermose |
| Solution 3 | cskwg |
| Solution 4 | Kristoffer Jälén |
| Solution 5 | EricS |
| Solution 6 | nAviD |
| Solution 7 | Mthokozisi Nyoni |
| Solution 8 | Alex |
