如何在 blazor(.razor page) 中设置默认类型 @typeparam TValue = bool

M K*_*M K 6 blazor blazor-server-side

我们在 Checkbox 组件的 selected 属性中提供了 TValue 支持。如何在 blazor(.razor page) 中设置默认类型 @typeparam TValue = bool

@using Microsoft.AspNetCore.Components.Web;
@using Microsoft.AspNetCore.Components.Rendering
@inherits BaseComponent;
@implements ICheckBox;
@typeparam TValue = bool;
Run Code Online (Sandbox Code Playgroud)

Abd*_*rah 8

这是一个已知问题 https://github.com/dotnet/aspnetcore/issues/13619

但我在项目中所做的工作是

public class DateTimePickerComponent : DateTimePickerComponent<DateTime?> { } // default Type value

public class DateTimePickerComponent<T> : BaseSubComponent { .... } // all business here

Run Code Online (Sandbox Code Playgroud)

在组件 I 中继承自其中之一

@inherits DateTimePickerComponent  // default type will be DateTime?
@inherits DateTimePickerComponent<DateTime> // this also work
Run Code Online (Sandbox Code Playgroud)

  • 据我所知,这会阻止您稍后在引用所使用的组件时将类型设置为另一种类型。我相信提供默认值的目的是仅当您没有要提供的显式类型时才允许您默认它。尽管如此,我还是能看出这在某些情况下会有什么用处。 (2认同)