Passing method to component
This is because the Click event of btnClick isn't of the type Action<int>
but actually EventCallback<TIn>
. So change you'll need to change a few things around.
change ExternalMethod to
[Parameter]
protected EventCallback<int> ExternalMethod {get; set;}
and change the btnClick to
public void btnClick(int param) { /* code */ }
// and then set the razor to
<TestMethodPassing ExternalMethod="@btnClick"></TestMethodPassing>
// Or do it with a lambda in the razor
<TestMethodPassing ExternalMethod="@((param) => {/* code */ })"></TestMethodPassing>
There is a GitHub issue tracking the new Event Handling and Binding here
Here is an example of passing a method from a parent to a child and the child invoking it. As you don't require a return value I'm just using Action
rather than Action<T>
.
There are many ways you can make this code more compact, but I've gone for a more verbose example to hopefully show what's happening a bit better.
Parent Component:
@page "/"
<h1>Hello, world!</h1>
Welcome to your new app.
<Child ParentMethod="@SayHello" />
@code {
private void SayHello()
{
Console.WriteLine("Hello!");
}
}
Child Component:
<h3>Child</h3>
<button @onclick="@InvokeParentMethod">Click Me!</button>
@code {
[Parameter] public Action ParentMethod { get; set; }
private void InvokeParentMethod()
{
ParentMethod?.Invoke();
}
}