简体   繁体   中英

Multiple parameters with EventCallback in Blazor without creating a class and pass it

One year go a created a project where i passed multiple parameters following the answers on this question: Blazor EventCallback with Multiple Params - how to respond to event in parent host control

When i tried now in .net 6 is not working(it is working with creating a class and passing the parameters to the fiels there, but for me is not the clean solution).

Parent component:

<MultipleParamsComp OnHandle="@((args) => await HandleParams(((int, string))args))" />

<p>@BaseId</p>
<p>@BaseString</p>


@code {
    private int BaseId = 0;
    private string BaseString = string.Empty;


    private async Task HandleParams((int, string) args)
    {
        BaseId = args.Item1;
        BaseString = args.Item2;
    }
}

Child Component:

<h4>Child Component</h4>


<button class="btn btn-primary" @onclick="(() => HandleMultipleParams(id))">
    Testing
</button>


@code {
    private int id = 0;
    private string Name = "something";

    [Parameter]
    public EventCallback<(int, string)> OnHandle { get; set; }

    private async Task HandleMultipleParams(int customerId)
    {
        customerId++;

        await OnHandle.InvokeAsync((customerId, Name));
    }
}

Output

Severity Code Description Project File Line Suppression State Error CS1001 Identifier expected BlazorTheory E:\Programare\Blazor\BlazorTheory\Microsoft.NET.Sdk.Razor.SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Theory_EventCallBack_Other_Examples_Passing_multiple_params_in_function_BaseComponent1_razor.g.cs 90 Active

Severity Code Description Project File Line Suppression State Error CS0119 'string' is a type, which is not valid in the given context BlazorTheory E:\Programare\Blazor\BlazorTheory\Microsoft.NET.Sdk.Razor.SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Theory_EventCallBack_Other_Examples_Passing_multiple_params_in_function_BaseComponent1_razor.g.cs 90 Active

Severity Code Description Project File Line Suppression State Error CS0119 'int' is a type, which is not valid in the given context BlazorTheory E:\Programare\Blazor\BlazorTheory\Microsoft.NET.Sdk.Razor.SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Theory_EventCallBack_Other_Examples_Passing_multiple_params_in_function_BaseComponent1_razor.g.cs 90 Active

Severity Code Description Project File Line Suppression State Error CS0119 'EventCallback' is a type, which is not valid in the given context BlazorTheory E:\Programare\Blazor\BlazorTheory\Microsoft.NET.Sdk.Razor.SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Theory_EventCallBack_Other_Examples_Passing_multiple_params_in_function_BaseComponent1_razor.g.cs 90 Active


Maybe i lack some theory of delegates or EventCallback and how they work behind the scene(any good documentation links helps).

Ty u.

Please take a look at following question- Blazor EventCallback with Multiple Params - how to respond to event in parent host control

await NewDayScrolledIntoView.InvokeAsync((p1, p2, p3));

<ShiftCardScroller NewDayScrolledIntoView="@((args)=> NewDayInView(args.Item1,args.Item2,args.Item3))" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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