我们可以在 Blazor 平台中借助 typeparam 分配和重用 TValue。但是,我们将如何为 Blazor InputNumber 组件动态分配 TValue?
代码示例:
[索引.剃刀]
<EditForm>
@*The below definiton is working*@
<InputNumber TValue="int?" @bind-Value="@DynamicModelInstance.ValueAsT"></InputNumber>
@*The below definiton is not working*@
<InputNumber TValue="DynamicModelInstance.Type" @bind-Value="@DynamicModelInstance.ValueAsT"></InputNumber>
</EditForm>
@code {
public DynamicModel<int> DynamicModelInstance { get; set; }
protected override void OnInitialized()
{
DynamicModelInstance = new DynamicModel<int>();
DynamicModelInstance.ValueAsT = 500;
}
}
[动态模型.cs]
namespace CustomComponent.Pages
{
public class DynamicModel<T> where T : struct
{
public System.Type Type { get; set; }
public bool Enabled { get; set; }
public DynamicModel()
{
this.Type = typeof(T);
}
private T _value;
public T ValueAsT
{
get { return (T)_value; }
set { this._value = value; }
}
}
}
如何达到这个要求?