简体   繁体   English

在 Blazor 中,如何使用 EventCallback 创建接口?

[英]In Blazor, how can I create an interface with EventCallback?

In Blazor, the EventCallback struct is a bit different from standard events, being single-cast instead of multi-cast, etc. For that reason, I don't think this is a duplicate.在 Blazor 中,EventCallback 结构与标准事件有点不同,是单播而不是多播等。因此,我不认为这是重复的。

I am trying to create a set of components that all have the same [parameter] EventCallback, and it seems reasonable to use an interface to enforce that... So far this doesn't seem to be possible.我正在尝试创建一组都具有相同[参数] EventCallback 的组件,并且使用接口来强制执行它似乎是合理的......到目前为止,这似乎是不可能的。

Things I've tried:我尝试过的事情:

Treating this like EventHandler像 EventHandler 一样对待它

public interface IResolver
{
    event EventCallback Done;
}

This raises the error: Event must be a delegate type.这会引发错误:事件必须是委托类型。

...And other variations on trying to make this work like an EventHandler. ...以及尝试使这项工作像 EventHandler 一样的其他变体。

Trying to use IEventCallback尝试使用 IEventCallback

EventCallback implements IEventCallback, so this seemd promising. EventCallback 实现了 IEventCallback,所以这看起来很有希望。 However, it is an internal interface, and is not available to me.但是,它是一个内部接口,对我不可用。

Using a base class instead使用基础 class 代替

This seems like it might work, but I am curious why I can't do this with an interface.这似乎可行,但我很好奇为什么我不能用界面来做到这一点。

Also found:还发现:

  • Event must be of delegate type? 事件必须是委托类型?
  • Various articles about implementing event-like constructs using interfaces, or help with EventHandler, which is not the same thing.关于使用接口实现类事件构造的各种文章,或者帮助使用EventHandler,这不是一回事。

None of that seems to be related to this problem.这些似乎都与这个问题无关。

Declare the Callback using the correct syntax and it will work.使用正确的语法声明回调,它将起作用。

Here's my test code.这是我的测试代码。

using Microsoft.AspNetCore.Components;

namespace TestBlazorServer
{
    public interface ICallbackComponent
    {
        public EventCallback MyCallBack { get; set; }
    }
}
// TestCallback.razor
@namespace  TestBlazorServer

@implements ICallbackComponent
<h3>TestCallback</h3>

<button class="btn btn-danger" @onclick="() => this.CallCallback()">Test Me</button>

We now declare MyCallBack with [Parameter] .我们现在用[Parameter]声明MyCallBack

using Microsoft.AspNetCore.Components;

namespace TestBlazorServer
{
    public partial class TestCallback : ComponentBase, ICallbackComponent
    {
        [Parameter] public EventCallback MyCallBack { get; set; }

        private void CallCallback()
        {
            MyCallBack.InvokeAsync();
        }
    }
}

And we can declare multiple instances in index.razor .我们可以在index.razor中声明多个实例。

// index.razor
<TestCallback MyCallBack="Callback1"></TestCallback>

<TestCallback MyCallBack="Callback2"></TestCallback>

<TestCallback MyCallBack="Callback3"></TestCallback>

<div>Test:@called</div>

@code {
    private string called;

    private void Callback1()
        => called = "First";

    private void Callback2()
        => called = "Second";

    private void Callback3()
        => called = "Third";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM