简体   繁体   中英

EventCallback<dynamic> throws RuntimeBinderException

Using the default template I modified the Counter:

@code {
    private int currentCount = 0;

    private async void IncrementCount()
    {
        currentCount++;

        await UpdateCallback.InvokeAsync(new { Counter = currentCount });
    }

    [Parameter] public EventCallback<dynamic> UpdateCallback { get; set; }
}

And I am using it like this:

<Counter UpdateCallback="(d)=>MyCallback(d,42)"></Counter>

@code {

    public void MyCallback(dynamic val, int number)
    {

    }
}

This leads to

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Cannot implicitly convert type 'void' to 'object''

If I pass around a Tuple<dynamic> it works.

Why is that?

You invoked using InvokeAsync so it is expecting a method which will return task not void. Use this where you used your Counter component, i think that will solve your problem.

<Counter UpdateCallback="async (d)=> await MyCallback(d,42)"></Counter>

@code {

    public async Task MyCallback(dynamic val, int number)
    {

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM