I'm having trouble with the EventCallback
in new RC. I have a SelectEdit
component with SelectedValue
and SelectedValueChanged
parameters. I want for my component to support two way binding (bind-SelectedValue
) and event handlers (SelectedValueChanged=@"OnValueChanged"
). This has worked with Action
, but now in the new RazorComponents when I convert to EventCallback I get the errors:
CS1503 Argument 6: cannot convert from 'Microsoft.AspNetCore.Components.EventCallback' to 'Microsoft.AspNetCore.Components.EventCallback
'
Error CS1660 Cannot convert lambda expression to type 'EventCallback
' because it is not a delegate type
SelectEdit.cshtml signature
[Parameter] protected TValue SelectedValue { get; set; }
[Parameter] protected EventCallback<TValue> SelectedValueChanged { get; set; }
Users should be able to use both ways of handling the component values, and not just the bind- attributes. Custom actions should also be able to use if necessary.
<SelectEdit SelectedValue="@someValue" SelectedValueChanged="@((v) => someValue=v)">
or
<SelectEdit SelectedValue="@someValue" SelectedValueChanged="@OnSomeValueChanged">
@functions{
void OnSomeValueChanged(string v)
{
someValue = v;
Console.WriteLine(someValue);
// do some other actions
}
}
I have another component that is also using EventCallback but this one is not generic. On that components I can use events handler without any error. eg.
SimpleButton.cshtml
[Parameter] protected EventCallback Clicked { get; set; }
Usage
<SimpleButton Clicked="@RaiseBasicCounter">Count</SimpleButton>
@functions{
int basicEventCounter = 0;
void RaiseBasicCounter()
{
basicEventCounter++;
}
}
Event callback is working as long the component is not generic. The following examples are basically the same, the only difference is that one is normal component that have a defined value type and the other is generic. I don't know why it will not work because EventCallback
is also a generic type.
<NumericEdit Value="5.8" ValueChanged="@((v)=>Console.WriteLine(v))" />
This implementation is NOT working!!
NumericEdit.cshtml
@typeparam TValue
@inherits BaseNumericEdit<TValue>
<input type="number" value="@Value" onchange="@HandleOnChange" oninput="@HandleOnInput" />
BaseNumericEdit.cs
public abstract class BaseNumericEdit<TValue> : BaseTextInput<TValue>
{
[Parameter] protected TValue Value { get => InternalValue; set => InternalValue = value; }
[Parameter] protected EventCallback<TValue> ValueChanged { get; set; }
}
This implementation is working!!
NumericEdit.cshtml
@inherits BaseNumericEdit
<input type="number" value="@Value" onchange="@HandleOnChange" oninput="@HandleOnInput" />
BaseNumericEdit.cs
public abstract class BaseNumericEdit : BaseTextInput<decimal>
{
[Parameter] protected decimal Value { get => InternalValue; set => InternalValue = value; }
[Parameter] protected EventCallback<decimal> ValueChanged { get; set; }
}
This is also observed in the native InputNumber razor component. It only allows binding through the bind-Value method.
I have the same issue with generic components and EventCallback
Is this intentional or a bug?
Thanks for contacting us, @stsrki.
@rynowak, can you please look into this? Thanks!
I tested this out, and it looks like we're not getting the right behavior. I'm not sure at this point if it's a codegen issue or if we're missing an overload.
obj\Debug\netcoreapp3.0\Razor\Pages\Index.razor.g.cs(26,202): error CS0029: Cannot implicitly convert type 'Microsoft.AspNetCore.Components.UIWheelEventArgs' to 'int' [C:\Users\rynowak\source\repos\BlazorInvestigate\BlazorInvestigate.csproj]
obj\Debug\netcoreapp3.0\Razor\Pages\Index.razor.g.cs(26,185): error CS1643: Not all code paths return a value in lambda expression of type 'Func<object, Task>' [C:\Users\rynowak\source\repos\BlazorInvestigate\BlazorInvestigate.csproj]
obj\Debug\netcoreapp3.0\Razor\Pages\Index.razor.g.cs(26,202): error CS0266: Cannot implicitly convert type 'object' to 'int'. An explicit conversion exists (are you missing a cast?) [C:\Users\rynowak\source\repos\BlazorInvestigate\BlazorInvestigate.csproj]
This has VS tooling impact that we will need to coordinate with.
Possibly related: #8336
This has been added in preview 7 - https://github.com/aspnet/AspNetCore/pull/10730
Note that preview 6 is the next release so you will have to wait a few weeks.
I have installed Preview 7 and I still get the error for generic EventCallback
@MichaelPeter I can also confirm that this bug is not fully fixed in preview 7. That is, it seems that it is working only if I specifically define generic type
This will giving me error because TValue is not defined:
<SelectEdit SelectedValue="@someValue" SelectedValueChanged="@((v) => someValue=v)">
Error CS1643 Not all code paths return a value in lambda expression of type 'Func
This is working only after TValue is set. It is not recognized from the context.
<SelectEdit TValue="int" SelectedValue="@someValue" SelectedValueChanged="@((v) => someValue=v)">
Hi, it looks like you are posting on a closed issue/PR/commit!
We're very likely to lose track of your bug/feedback/question unless you:
Thanks!
May be related/duplicate of https://github.com/aspnet/AspNetCore/issues/12226
Most helpful comment
I have installed Preview 7 and I still get the error for generic EventCallback Error cannot convert from 'method group' to 'EventCallback' #10077 10077