简体   繁体   English

具有动态变化的事件回调 class?

[英]Eventcallback with dynamic change class?

I have some circumstates, when i have some list with classes take example like this List<Crews> and List<CrewsOnLeave> , and i made some component where i will be using the logic there and just sends one of those two (either CrewsOnLeave or Crews), so i don't need to declare my class in the component, because here is my code我有一些情况,当我有一些带有类的列表时,例如List<Crews>List<CrewsOnLeave> ,我制作了一些组件,在那里我将使用那里的逻辑并只发送这两个中的一个(CrewsOnLeave 或船员),所以我不需要在组件中声明我的 class,因为这是我的代码

My Code Right now我现在的代码

    @using Models.BookingCrew

    @if (CrewonLeave.Count() > 0)
    {
    <td @onclick="SearchCrews">
        <span style="cursor: pointer;" data-toggle="modal"
            data-target="#@(modalId)">
            @CrewonLeave.Count()</span>
    </td>
    }
    else if(Crews.Count()> 0 )
    {
        <td @onclick="SearchOnLeave">
        <span style="cursor: pointer;" data-toggle="modal"
            data-target="#@(modalId)">
            @Crews.Count()</span>
    </td>
    }
    else
    {
    <td> </td>
    }

    @code{

        [Parameter]
        public string modalId{get;set;}

        [Parameter]
        public List<Crew> Crews{get; set;}
        [Parameter]
        public List<CrewOnLeave> CrewonLeave{get;set;}
        [Parameter]
        public EventCallback<List<CrewOnLeave>> OnLeaveClicked {get; set;}
        [Parameter]
        public EventCallback<List<Crew>> OnCrewsClicked {get; set;}
        private Task SearchCrews()
        {
            return OnCrewsClicked.InvokeAsync(Crews);
        }
        private Task SearchOnLeave()
        {
            return OnLeaveClicked.InvokeAsync(CrewonLeave);
        }

    }

My goals are to make my code more simpler like this我的目标是让我的代码像这样更简单

Code Goal代码目标

    @if (Crews.Count() > 0)
    {
    <td @onclick="SearchCrews">
        <span style="cursor: pointer;" data-toggle="modal"
            data-target="#@(modalId)">
            @Crews.Count()</span>
    </td>
    }
    else
    {
    <td> </td>
    }

    @code{

        [Parameter]
        public string modalId{get;set;}

        [Parameter]
        public List<DynamicClass> Crews{get; set;}
        [Parameter]
        public EventCallback<List<DynamicClass>> OnCrewsClicked {get; set;}
        private Task SearchCrews()
        {
            return OnCrewsClicked.InvokeAsync(DynamicClass);
        }
    }

The Problem is i don't know how to put into that dynamic class so the class will be dynamic when component inputed by variable list with a class.问题是我不知道如何将dynamic class放入,因此当使用 class 的变量列表输入组件时,class 将是动态的。

Is there any way to make it like that?有没有办法让它变成这样?

It all depends on the relationship between CrewOnLeave and Crew .这完全取决于CrewOnLeaveCrew之间的关系。 If they aren't two incarnations of the same base class then you need to define an interface that the both share.如果它们不是同一个基础 class 的两个化身,那么您需要定义一个两者共享的接口。 In the example the interface is blank which still works.在示例中,界面是空白的,仍然可以使用。

There's a simplified version of your code below, with to demo various methods of dealing with interface to class and class to interface conversions.下面是您的代码的简化版本,用于演示处理 class 和 class 到接口转换的接口的各种方法。


@page "/Crewzing"
<table>
    <tr>
        @if (_hasCrews)
        {
            <td @onclick="SearchCrews">
                <span style="cursor: pointer;" data-toggle="modal"
                      data-target="#@(modalId)">
                    @Crews.Count()
                </span>
            </td>
        }
        else
        {
            <td></td>
        }

    </tr>
</table>

@code {

    public List<ICrew> Crews => CrewsOnLeave.Count > 0 ? CrewsOnLeave.Select(item => (ICrew)item).ToList() : CrewsOnBoard.Select(item => (ICrew)item).ToList();

    [Parameter] public EventCallback<List<ICrew>> CrewsClicked { get; set; }

    private bool _hasCrews => (this.Crews != null && this.Crews.Count > 0);

    private string modalId = "xxx";

    private void SearchCrews()
    {
        GetCrewType();
        CrewsClicked.InvokeAsync(Crews);
    }

    public interface ICrew
    {
    }

    public class CrewOnBoard : ICrew
    {
        public string Boat { get; set; }
    }

    public class CrewOnLeave : ICrew
    {
        public string Boat { get; set; }
    }

    public List<CrewOnBoard> CrewsOnBoard = new List<CrewOnBoard>()
{
        new CrewOnBoard() { Boat = "Cyclops"},
        new CrewOnBoard() { Boat = "Sirius"}
    };

    public List<CrewOnLeave> CrewsOnLeave = new List<CrewOnLeave>()
{
        new CrewOnLeave() { Boat = "Medusa"},
        new CrewOnLeave() { Boat = "Alderbaran"}
    };

    private void GetCrewType()
    {
        var c = Crews[0] ?? null;
        if (c is CrewOnBoard)
        {
            var crew = Crews.Select(item => (CrewOnBoard)item).ToList();
        }
        else if (c is CrewOnLeave)
        {
            var crew = Crews.Select(item => (CrewOnLeave)item).ToList();
        }
    }
}

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

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