如何制作具有绑定值/类型param的wrapper-around组件?关于mudblazor mudselect



我需要围绕mudblazor组件构建一些包装器

假设我有基本的考试,比如:

<MudSelect T="int" @bind-Value="@model.dict1" For="@(() => @model.dict1)" AnchorOrigin="Origin.BottomCenter">                
<MudSelectItem T="int"  Value="1">1</MudSelectItem>
<MudSelectItem T="int" Value="2">2</MudSelectItem>
</MudSelect>

所以现在我想做一些像一样的包装

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

因此,基于参数TableName,我想在选择列表中呈现不同的项目

现在我有:

DictMudSelectComponent.razor
@typeparam T
<MudSelect T="@T" @bind-Value="@Value" For="()=>For()" @attributes=AllOtherAttributes>
<MudSelectItem T="int"  Value="1">1</MudSelectItem>
<MudSelectItem T="int"  Value="2">2</MudSelectItem>
<MudSelectItem T="int"  Value="3">3</MudSelectItem>
@code {
private Func<T> _for ;
[Parameter]
public Func<T> For
{
get => _for;
set
{
if (_for == value)
return;
_for = value;
}
}
private T _value ;
[Parameter]
public T Value
{
get => _value;
set => _value = value;
}
[Parameter]
public string TableName { get; set; } = "";

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

但即使这样也不起作用——为什么?它编译得很好,但在渲染时我得到了

Unhandled exception rendering component: Unable to set property 'AnchorOrigin' on object of type 'MudBlazor.MudSelect`1[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]'. The error was: Specified cast is not valid.
System.InvalidOperationException: Unable to set property 'AnchorOrigin' on object of type 'MudBlazor.MudSelect`1[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]'. The error was: Specified cast is not valid.
---> System.InvalidCastException: Specified cast is not valid.
at Microsoft.AspNetCore.Components.Reflection.PropertySetter.CallPropertySetter[MudSelect`1,Origin](Action`2 setter, Object target, Object value)
at Microsoft.AspNetCore.Components.Reflection.PropertySetter.SetValue(Object target, Object value)
at Microsoft.AspNetCore.Components.Reflection.ComponentProperties.<SetProperties>g__SetProperty|3_0(Object target, PropertySetter writer, String parameterName, Object value)
--- End of inner exception stack trace ---
at Microsoft.AspNetCore.Components.Reflection.ComponentProperties.<SetProperties>g__SetProperty|3_0(Object target, PropertySetter writer, String parameterName, Object value)
at Microsoft.AspNetCore.Components.Reflection.ComponentProperties.SetProperties(ParameterView& parameters, Object target)
at Microsoft.AspNetCore.Components.ParameterView.SetParameterProperties(Object target)
at Microsoft.AspNetCore.Components.ComponentBase.SetParametersAsync(ParameterView parameters)
at MudBlazor.MudBaseInput`1[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].<>n__1(ParameterView parameters)
at MudBlazor.MudBaseInput`1.<SetParametersAsync>d__177[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()

我在这里做错了什么?感谢并问候

好的,所以第一个问题是它需要是

<MudSelect  @bind-Value="@Value"  For="@For" >

并且在@代码中

private Expression<Func<TypeParam>>? _for;
[Parameter]
public Expression<Func<TypeParam>> For
{
get => _for!;
set
{
if (_for == value)
return;
_for = value;
}
}

然后它运行时没有错误,但我无法更改值-它永远保持在0/默认值-为什么?我现在没有选择这个硬编码1/2/3-总是回到0/默认值有什么建议吗?regads

编辑好的,第二个问题是

[Parameter]
public T? Value
{
get => _value;
set 
{ 
if (!Equals(value, _value))
{
_value = value;
if (ValueChanged.HasDelegate)
ValueChanged.InvokeAsync(_value);
}
}
}

如果相等,则必须检查它,因为如果不相等,则问题是MudSelect正在触发参数上的Setter,然后它调用回调,然后它再次调用参数上的Setter。

相关内容

  • 没有找到相关文章

最新更新