如何在Blazor(.razor页面)中设置默认类型@typeparam TValue = bool?

6
我们在Checkbox组件的checked属性中提供了TValue支持。如何在blazor(.razor页面)中设置默认类型为@typeparam TValue = bool。
@using Microsoft.AspNetCore.Components.Web;
@using Microsoft.AspNetCore.Components.Rendering
@inherits BaseComponent;
@implements ICheckBox;
@typeparam TValue = bool;

3
答案非常好,你应该接受它。 - henon
1个回答

8

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

但我在一个项目中使用的解决方法

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

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

在组件中,我从其中之一继承

@inherits DateTimePickerComponent  // default type will be DateTime?
@inherits DateTimePickerComponent<DateTime> // this also work

2
据我所知,这会防止您在引用已使用的组件时将类型稍后设置为另一种类型。我认为提供默认值的目的是允许您仅在没有显式类型可提供时才使用默认值。尽管如此,我可以看出在某些情况下这可能会很有用。 - misterbee180

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接