首页 > 解决方案 > 如何在 Blazor 中创建 Razor 页面列表

问题描述

我正在尝试创建剃须刀页面列表。
我解释自己,在我的“Index.razor”中,我实际上有:

@page "/"
@inject IJSRuntime JSRuntime

<h1 style="text-align:center;color:red" >Hey :)</h1>
<br />
<Counter/>
<NewCounter/>
<AnotherVersion/>

谁重定向到 Counter.razor、NewCounter.razor 和 AnotherVersion.razor。

但是,就像一个字符串列表,我想创建类似的东西:拥有这样的列表:

List<Page> myPage = new List<Page>{Counter, NewCounter, AnotherVersion};
@page "/"
@inject IJSRuntime JSRuntime

<h1 style="text-align:center;color:red" >Hey :)</h1>
<br />
@for(int i = 0; i < myPage.Count; i++)
{
    myPage[i];
}

标签: c#asp.net.netrazorblazor

解决方案


好的,有人发布答案并立即将其删除。

所以,这里的答案:

@page "/"
@inject IJSRuntime JSRuntime

<h1 style="text-align:center;color:@bgColor" >My Dashboard</h1>
<br />
@foreach (var component in widget)
{
    <DynamicComponent Type=@component />
}

@code {
    public System.Type[] widget =
    new[] { typeof(Counter), typeof(NewCounter), typeof(AnotherVersion) };
}

(如果他转发他的消息,我会删除我的)


推荐阅读