我正在学习 Blazor 和 ASP.NET,并已经学习了6个月的 C#。
我制作了一个简单的模板组件:
@typeparam GenericType
<ul>
@foreach (GenericType item in Items)
{
<li @onclick="(x)=> ItemClicked(item)">FragmentToRender(item)</li>
}
</ul>
@code {
[Parameter] public RenderFragment<GenericType> FragmentToRender { get; set; }
[Parameter] public IReadOnlyList<GenericType> Items { get; set; }
public void ItemClicked(GenericType item)
{
//To figure out...
}
}
我正在页面组件中使用它:
<TestComponent GenericType="Thing" Items="ListOfThings">
<FragmentToRender>
<p>@context.Field</p>
</FragmentToRender>
</TestComponent>
@code
{
private List<Thing> ListOfThings =
new List<Thing> {
new Thing("Test"),
new Thing("Test2")
};
public class Thing
{
public readonly string Field;
public Thing(string field) => Field = field;
}
}