Trouble passing eventcallback to component which inherits InputBase<TValue>

116 views Asked by At

I am developing a custom select component which is inherited from, InputBase as follows:

Index.razor:

<Select Id="InvestmentEntity"
    OnSelected="@OnInvestmentEntitySelect"
</Select>

Index.razor.cs

public void OnInvestmentEntitySelect(Select<int> sender)
{
    // .. do something later
}

Select.razor.cs

namespace Accounting.Web.Components.Forms
{
    public partial class Select<TValue> : InputBase<TValue>
    {
        [Parameter]
        public EventCallback<Select<TValue>> OnSelected { get; set; } = default!;


        protected override bool TryParseValueFromString(string? value, [MaybeNullWhen(false)] out TValue result, [NotNullWhen(false)] out string? validationErrorMessage)
        {            
            if (BindConverter.TryConvertTo<TValue>(value, null, out result))
            {
                validationErrorMessage = "";
                return true;
            }
            else
            {
                validationErrorMessage = "Err : Select value";
                return false;
            }
        }

        public void OnSelect(SelectOption? option)
        {
            OnSelected.InvokeAsync(this);
        }

    }
}

As you can see I am attempting to pass a callback event via the component tag (OnSelected) which is invoked the Select.razor.cs file. However the compiler is returning the following error in relation to this line:

OnSelected="@OnInvestmentEntitySelect"

Error:

Argument 2: cannot convert from 'method group' to 'EventCallback'

If I try the following, I get a different error:

OnSelected="@(() => OnInvestmentEntitySelect)"

Errors:

Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
'OnInvestmentEntitySelect' to non-delegate type 'Task'. Did you intend to invoke the method?
Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type     

How can I pass the method to the even callback parameter so that it is called when invoked by the component in the scenario above? My objective is to pass the reference of the instance of the component which invoked the event back as a parameter to the callback method in the parent page

0

There are 0 answers