Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EventCallback not working as expected #8385

Closed
stsrki opened this issue Mar 10, 2019 · 12 comments · Fixed by #10730
Closed

EventCallback not working as expected #8385

stsrki opened this issue Mar 10, 2019 · 12 comments · Fixed by #10730
Assignees
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. Done This issue has been fixed

Comments

@stsrki
Copy link
Contributor

stsrki commented Mar 10, 2019

Describe the bug

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; }

Expected behavior

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
    }
}

Additional context

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++;
    }
}
@Eilon Eilon added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Mar 10, 2019
@stsrki
Copy link
Contributor Author

stsrki commented Mar 21, 2019

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.

Usage

<NumericEdit Value="5.8" ValueChanged="@((v)=>Console.WriteLine(v))" />

Generic Component

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; }
}

Ordinal Component

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; }
}

@ivanchev
Copy link

This is also observed in the native InputNumber razor component. It only allows binding through the bind-Value method.

@mwinkler
Copy link

I have the same issue with generic components and EventCallback.
Is this intentional or a bug?

@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label Mar 26, 2019
@mkArtakMSFT
Copy link
Member

Thanks for contacting us, @stsrki.
@rynowak, can you please look into this? Thanks!

@rynowak rynowak added the bug This issue describes a behavior which is not expected - a bug. label Apr 10, 2019
@rynowak rynowak removed their assignment Apr 10, 2019
@rynowak
Copy link
Member

rynowak commented Apr 10, 2019

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]

@danroth27 danroth27 added this to the 3.0.0-preview5 milestone Apr 12, 2019
@danroth27
Copy link
Member

This has VS tooling impact that we will need to coordinate with.

@mkArtakMSFT mkArtakMSFT removed area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates labels May 9, 2019
@mkArtakMSFT mkArtakMSFT assigned rynowak and unassigned javiercn May 17, 2019
@chucker
Copy link

chucker commented May 17, 2019

Possibly related: #8336

@rynowak rynowak removed the Working label May 29, 2019
rynowak pushed a commit that referenced this issue Jun 1, 2019
Fixes: #8493
Fixes: #9632
Fixes: #9339
Fixes: #8385
Fixes: 10077

This fix adds support for type converters as well as a few other minor
things we were missing from binding. The key thing about supporting
conversions is that we new can support arbitrary types with `@bind`.
This means you can use it with generics, which is something many users
have tried.

Along with type converters we get Guid and TimeSpan from the BCL. The
BCL also includes converters for types we're less interested in like
`short`.
rynowak pushed a commit that referenced this issue Jun 3, 2019
Fixes: #8493
Fixes: #9632
Fixes: #9339
Fixes: #8385
Fixes: 10077

This fix adds support for type converters as well as a few other minor
things we were missing from binding. The key thing about supporting
conversions is that we new can support arbitrary types with `@bind`.
This means you can use it with generics, which is something many users
have tried.

Along with type converters we get Guid and TimeSpan from the BCL. The
BCL also includes converters for types we're less interested in like
`short`.
rynowak added a commit that referenced this issue Jun 5, 2019
* Add support for TypeConverter

Fixes: #8493
Fixes: #9632
Fixes: #9339
Fixes: #8385
Fixes: 10077

This fix adds support for type converters as well as a few other minor
things we were missing from binding. The key thing about supporting
conversions is that we new can support arbitrary types with `@bind`.
This means you can use it with generics, which is something many users
have tried.

Along with type converters we get Guid and TimeSpan from the BCL. The
BCL also includes converters for types we're less interested in like
`short`.

* Use correct NumberStyles

* Fix culture

* Core check
@rynowak
Copy link
Member

rynowak commented Jun 5, 2019

This has been added in preview 7 - #10730
Note that preview 6 is the next release so you will have to wait a few weeks.

@MichaelPeter
Copy link

I have installed Preview 7 and I still get the error for generic EventCallback Error cannot convert from 'method group' to 'EventCallback' #10077 10077

@stsrki
Copy link
Contributor Author

stsrki commented Jul 26, 2019

@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

Not working:

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<object, Task>'
Error CS0266 Cannot implicitly convert type 'object' to 'int'. An explicit conversion exists (are you missing a cast?)
Error CS0029 Cannot implicitly convert type 'Microsoft.AspNetCore.Components.UIWheelEventArgs' to 'int'

Working:

This is working only after TValue is set. It is not recognized from the context.

<SelectEdit TValue="int" SelectedValue="@someValue" SelectedValueChanged="@((v) => someValue=v)">

@rynowak
Copy link
Member

rynowak commented Jul 26, 2019

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:

  1. Open a new issue
  2. Explain very clearly what you need help with
  3. If you think you have found a bug, include detailed repro steps so that we can investigate the problem

Thanks!

@marin-bratanov
Copy link

May be related/duplicate of #12226

@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. Done This issue has been fixed
Projects
None yet
Development

Successfully merging a pull request may close this issue.