Blazor将EventCallback作为类属性传递给组件

时间:2020-10-16 08:32:00

标签: c# blazor

我正在尝试在Blazor中开发一个组件,我希望它从C#类中获取其所有功能(文本,EventCallbacks等)。像这样:

Blazor组件(后面的代码):

public partial class Card : ComponentBase
{
    [Parameter] public CardData CardData { get; set; }

    protected async Task OnActionFiredEvent(EventCallback<object> eventCallback, object itemId)
    {
        if (eventCallback.HasDelegate)
            await CardData.CardTitleOnClickAction.InvokeAsync(itemId);
    }
}

Blazor组件(剃刀):

<!-- Card image -->
<div class="view view-cascade overlay">
    <a>
        <div class="mask rgba-white-slight waves-effect waves-light"></div>
    </a>
</div>

<!-- Card content -->
<div class="card-body card-body-cascade text-center">

    <!-- Title -->
    <h4 class="card-title">
        <a class="waves-effect waves-light" @onclick="@(() => OnActionFiredEvent(CardData.CardTitleOnClickAction, CardData.CardItemId))">
            <strong>@CardData.CardTitle</strong>
        </a>
    </h4>
    <!-- Subtitle -->
    @if (!string.IsNullOrEmpty(CardData.CardSubTitle))
    {
        <h6 class="font-weight-bold indigo-text py-2">@CardData.CardSubTitle</h6>
    }

    <!-- Text -->
    @if (!string.IsNullOrEmpty(CardData.CardText))
    {
        <p class="card-text">
            @CardData.CardText
        </p>
    }
</div>

<!-- Card footer -->
<div class="card-footer text-muted text-center">
    @CardData.CardFooter
</div>

CarData类:

public class CardData
{
    public object CardItemId { get; set; }
    public string CardTitle { get; set; }
    public string CardSubTitle { get; set; }
    public string CardText { get; set; }
    public string CardFooter { get; set; }
    public EventCallback<object> CardTitleOnClickAction { get; set; }
}

使用:

<Card CardData="@(new CardData() {
CardTitle = "TextTitle",
CardTitleOnClickAction = EventCallBackMethod,
CardSubTitle = "TextSubTitle",
CardText = "Text"})" />

问题是当将EventCallBack作为类属性传递时,出现以下错误: 无法将方法组“ EventCallBackMethod”转换为非委托类型“ EventCallback”。您打算调用该方法吗?

失败的方法本身是这样的:

protected async Task EventCallBackMethod()
    {
        await DoSomething();
    }

有什么办法解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

正如布赖恩·帕克(Brian Parker)所述,您需要在回调中输入类型。例如,如果您不想传递值,则可以传递null

MyComponent.razor

<button @onclick="OnButtonClicked">Click me</button>

@code{
    [Parameter]
    public EventCallback ButtonClicked{ get; set; }


    protected async Task OnButtonClicked()
    {
        await ButtonClicked.InvokeAsync(null);
    }
}

例如,作为剃须刀组件,按钮单击触发OnButtonClicked,然后调用ButtonClicked参数,该参数可以在父页面中引用。

Page.razor

@page "/pagename"

<MyComponent ButtonClicked="@ButtonClickedHandler" />

@code{
    protected void ButtonClickedHandler()
    {
        Logger.LogInfo("Button click was handled on the page");
    }
}

如果您想传递参数,可以按照以下步骤进行;

MyNextComponent.razor

<button @onclick="OnButtonClicked">Click me</button>

@code{
    [Parameter]
    public EventCallback<bool> ButtonClicked{ get; set; }

    protected async Task OnButtonClicked(bool value)
    {
        await ButtonClicked.InvokeAsync(true);
    }
}

Page.razor @page“ / pagename”

<MyNextComponent ButtonClicked="@ButtonClickedHandler" />

@code{
    protected void ButtonClickedHandler(bool value)
    {
        Logger.LogInfo($"Button click was handled on the page: {value}");
    }
}

答案 1 :(得分:0)

像这样创建您的 EventCallback

    private async Task Press1()
    {
        this.message = "F1";
    }

EventCallback evt1 = EventCallback.Factory.Create(this, Press1);

财产

public class CardData
{
    public EventCallback<object> CardTitleOnClickAction { get; set; }
    public static EventCallback GetEvent(CardData data)
    {
        return data.CardTitleOnClickAction;
    }
}

成分

@onclick="() =>Modal.CardData.GetEvent(data).InvokeAsync()"