初学者 Blazor 问题:无法从 'method group' 转换为 'EventCallback'

Beginner Blazor issue: cannot convert from 'method group' to 'EventCallback'

我正在创建我的第一个 Blazor 应用程序,我有两个名为 child 和 parent 的组件:

child:

<div class="alert alert-primary" role="alert">
    @Title
</div>

<div class="alert alert-primary" role="alert">
    @ChildContent
</div>

<button type="button" class="btn btn-primary" @onclick="onClickMethod">Primary</button>


@code {
    [Parameter]
    public string Title { get; set; }

    [Parameter]
    public RenderFragment ChildContent { get; set; }

    [Parameter]
    public EventCallback onClickMethod { get; set; }
}

Parent:

@page "/childcomp"

<h3>ParentComponent</h3>
<BlazingShop.Pages.Components.ChildComponent OnClickMethod="@ShowMessage"
                                             Title="This title is from parent"> This is between component of the parent</BlazingShop.Pages.Components.ChildComponent>

<p><b>@messageText</b></p>

@code {
    private string messageText;
    private void ShowMessage()
    {
        messageText = "Blazing Text from Parent";
    }

}

在 Parent 部分,@ShowMessage" 是红色的,我看到了错误。我正在关注在线教程,这只发生在我这边。我该如何解决这个问题?

谢谢

您的 parent 有大小写问题。

在您的 child 组件中,EventCallback 属性 的名称为 onClickMethod。在 parent 上,您使用的是 OnClickMethod。这些属性区分大小写。

使用标准 C# 约定在组件中命名您的参数。

   [Parameter]
// public EventCallback onClickMethod { get; set; }
   public EventCallback OnClickMethod { get; set; }