【问题标题】:Blazor why CaptureUnmatchedValues not works as expected?Blazor 为什么 CaptureUnmatchedValues 不能按预期工作?
【发布时间】:2022-12-15 09:43:11
【问题描述】:

我正在围绕选择列表构建一些包装器。

让我们考虑一个基本的例子:

DictListComponent.razor:

 @using System.Linq.Expressions
 @typeparam T

 <MudSelect T="T" @bind-Value="Value" For="@For" AnchorOrigin="Origin.BottomCenter">
    <MudSelectItem T="int?" Value="null"> </MudSelectItem>
    <MudSelectItem T="int?" Value="1">1</MudSelectItem>
    <MudSelectItem T="int?" Value="2">2</MudSelectItem>
</MudSelect>

用法是

 <DictMudSelectComponent  @bind-Value="@model.dict1"  For="@(() => @model.dict1)" ></DictMudSelectComponent>

这可以。然后,如果我想更改为从父级传递此 AnchorOrigin,用法为:

<DictMudSelectComponent  @bind-Value="@model.dict1"  For="@(() => @model.dict1)"  AnchorOrigin="Origin.BottomCenter"></DictMudSelectComponent>

然后在DictListComponent.razor我添加

 <MudSelect T="T" @bind-Value="Value" For="@For" @attributes=AllOtherAttributes>
    <MudSelectItem T="int?" Value="null"> </MudSelectItem>
    <MudSelectItem T="int?" Value="1">1</MudSelectItem>
    <MudSelectItem T="int?" Value="2">2</MudSelectItem>
</MudSelect>

@code
{
 [Parameter(CaptureUnmatchedValues = true)]
 public Dictionary<string, object> AllOtherAttributes { get; set; } = new(); 
}

在运行时我得到

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
未处理的异常渲染组件:无法在类型为“MudBlazor.MudSelect`1[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]”的对象上设置属性“AnchorOrigin” .
错误是:指定的转换无效。

System.InvalidOperationException:无法在“MudBlazor.MudSelect`1[[System.Int32,System.Private.CoreLib,Version=6.0.0.0,Culture=neutral,PublicKeyToken=7cec85d7bea7798e]]”类型的对象上设置属性“AnchorOrigin”。错误是:指定的转换无效。

System.InvalidCastException:指定的转换无效。

在 Microsoft.AspNetCore.Components.Reflection.PropertySetter.CallPropertySetter[MudSelect1,Origin](Action2 设置器、对象目标、对象值)
在 Microsoft.AspNetCore.Components.Reflection.PropertySetter.SetValue(对象目标,对象值)
在 Microsoft.AspNetCore.Components.Reflection.ComponentProperties.g__SetProperty|3_0(对象目标、PropertySetter 编写器、字符串参数名称、对象值)
--- 内部异常堆栈跟踪结束 ---
在 Microsoft.AspNetCore.Components.Reflection.ComponentProperties.g__SetProperty|3_0(对象目标、PropertySetter 编写器、字符串参数名称、对象值)
在 Microsoft.AspNetCore.Components.Reflection.ComponentProperties.SetProperties(ParameterView& 参数,对象目标) 在 Microsoft.AspNetCore.Components.ParameterView.SetParameterProperties(对象目标) 在 Microsoft.AspNetCore.Components.ComponentBase.SetParametersAsync(ParameterView 参数) 在 MudBlazor.MudBaseInput1[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].&lt;&gt;n__1(ParameterView parameters) at MudBlazor.MudBaseInput1.d__177[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()

为什么这不起作用?我在这里错过了什么?

谢谢并恭祝安康 !

【问题讨论】:

    标签: c# .net blazor


    【解决方案1】:

    尝试:AnchorOrigin="@(Origin.BottomCenter)"

    因为我认为没有 @ 它只是作为字符串传递。

    【讨论】:

    • 你是对的,谢谢 ;) 但是为什么在原来的 <MudSelect T="T" @bind-Value="Value" For="@For" AnchorOrigin="Origin.BottomCenter"> @ 不需要?
    • 因为 MudSelect 将其声明为显式参数。 CaptureUnmatchedValues 不知道类型。
    猜你喜欢
    • 2021-05-30
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多