gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:26:30 24 4
gpt4 key购买 nike

我正在尝试在 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 组件(Razor):

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

汽车数据类:

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”转换为非委托(delegate)类型“EventCallback”。您是否打算调用该方法?

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

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

知道如何解决这个问题吗?

最佳答案

正如 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);
}
}

例如作为 Razor 组件,按钮点击触发 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 "/页面名称"

<MyNextComponent ButtonClicked="@ButtonClickedHandler" />

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

关于c# - Blazor 将 EventCallback 作为类属性传递给组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64385601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com