通用元组参数的 Blazor 问题
Blazor issue with generic Tuple-parameter
我有一个简单的组件,它扩展了一个第三方下拉组件。在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));
}
组件本身 (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; }
}
但是,在构建它时,我在生成的 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)>'
这是在 MyLanguagePicker.razor.g.cs
中引发错误的部分,MyLanguagePicker.razor.g.cs
是 MyDropDown
所使用的组件。
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)>>(
如您所见,它不知何故缺少一个空格,它应该是 string Value
而不是 stringValue
。 Value
是 MyDropDown
的通用部分。如果我将 Data
更改为 IEnumerable<(string Value, string Text)
,构建运行时不会出现任何错误。
如果我添加缺少的空格,它也可以正常运行。
更新以回答第一条评论
我已经尝试将其重命名为 TDropDownVal 并将名称更改为 Whatever
而不是 Value
,因为将元组值设置为 TValue Value
可能是个问题。但是 TDropDownVal Whatever
仍然在构建时抛出相同的错误。
当您在 MyDropDown.razor
中更改它时,编译错误就会消失。这表明 RadzenDropDown
不知道如何在运行时处理元组。它似乎想将其转换为字符串。
[Parameter]
public IEnumerable<(string , TValue )> Data { get; set; }
Razden 组件不知道如何将其转换为字符串以供显示。我对 Razden 组件一无所知,但如果它允许您呈现自己的内容,您也许可以管理它。
我有一个简单的组件,它扩展了一个第三方下拉组件。在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));
}
组件本身 (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; }
}
但是,在构建它时,我在生成的 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)>'
这是在 MyLanguagePicker.razor.g.cs
中引发错误的部分,MyLanguagePicker.razor.g.cs
是 MyDropDown
所使用的组件。
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)>>(
如您所见,它不知何故缺少一个空格,它应该是 string Value
而不是 stringValue
。 Value
是 MyDropDown
的通用部分。如果我将 Data
更改为 IEnumerable<(string Value, string Text)
,构建运行时不会出现任何错误。
如果我添加缺少的空格,它也可以正常运行。
更新以回答第一条评论
我已经尝试将其重命名为 TDropDownVal 并将名称更改为 Whatever
而不是 Value
,因为将元组值设置为 TValue Value
可能是个问题。但是 TDropDownVal Whatever
仍然在构建时抛出相同的错误。
当您在 MyDropDown.razor
中更改它时,编译错误就会消失。这表明 RadzenDropDown
不知道如何在运行时处理元组。它似乎想将其转换为字符串。
[Parameter]
public IEnumerable<(string , TValue )> Data { get; set; }
Razden 组件不知道如何将其转换为字符串以供显示。我对 Razden 组件一无所知,但如果它允许您呈现自己的内容,您也许可以管理它。