KingKerosin
KingKerosin

Reputation: 3841

Blazor issue with generic Tuple-parameter

I have a simple component, which is extending a third-party dropdown-component. It's called this way in MyLanguagePicker.razor

<MyDropDown TValue="string" Data="@Languages" OnChange="@OnSelectedAsync"  />

@code{
    private IEnumerable<(string, string)> Languages => _configuration.GetSection(Constants.Configuration.Keys.Cultures).GetChildren().Select(c => (c.Value, c.Key));
}

The component itself (MyDropDown.razor)

@inherits MyComponentBase<MyDropDown<TValue>>

@typeparam TValue

<RadzenDropDown TValue="TValue" Data="@Data" TextProperty="Text" ValueProperty="Value" />

@code{      
    [Parameter]
    public IEnumerable<(TValue Value, string Text)> Data { get; set; }
}

However, when building this I get an error in the generated cs-file

Error CS0246: The type or namespace name 'stringValue' could not be found (are you missing a using directive or an assembly reference?)

Error CS1503: Argument 1: cannot convert from 'System.Collections.Generic.IEnumerable<(string, string)>' to 'System.Collections.Generic.IEnumerable<(stringValue, string Text)>'

This is the part which throws the error in MyLanguagePicker.razor.g.cs which is the component the MyDropDown is used by.

public partial class MyLanguagePicker : Microsoft.AspNetCore.Components.ComponentBase
{
    #pragma warning disable 1998
    protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
    {
        __builder.OpenComponent<My.WebSite.Shared.RazorComponents.MyDropDown<string>>(0); // error on the end                                               ###########   
        __builder.AddAttribute(1, "Data", Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<System.Collections.Generic.IEnumerable<(stringValue, System.String Text)>>(

As you can see, it somehow is missing a whitespace, as it should be string Value instead of stringValue. The Value is the generic part of MyDropDown. If I change Data to IEnumerable<(string Value, string Text) the build runs without any error. If I add the missing whitespace it also runs without any errors.

Update to answer first comments

I already tried renaming it to TDropDownVal and changing the name to Whatever instead of Value as it might be an issue having the tuple-value as TValue Value. But TDropDownVal Whatever is still throwing the same error on build.

Upvotes: 0

Views: 1327

Answers (1)

Brian Parker
Brian Parker

Reputation: 14553

The compile error goes away when you change this in your MyDropDown.razor. This then shows that the RadzenDropDown does not know how to deal with a tuple at runtime. It appears to to want to convert it to a string.

[Parameter]
public IEnumerable<(string , TValue )> Data { get; set; }

The Razden Component does not know how to convert this to a string for display. I dont know anything about Razden components but you maybe able to manage this if it allows you to render your own content.

Upvotes: 1

Related Questions