c# - 如何在 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];
}
解决方案
好的,有人发布答案并立即将其删除。
所以,这里的答案:
@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) };
}
(如果他转发他的消息,我会删除我的)
推荐阅读
- python - Kivy 图像/删除图像
- javascript - 创造单元素
- c++ - 在加载有 dlopen 的库中使用 std::thread 会导致 sigsev
- c# - 由于查询字符串,无法将 MySqlConnection、MySqlCommand 等分离到另一个类中
- smtp - Grafana 警报测试通知已发送,但未收到电子邮件
- javascript - 反应传递道具父母给孩子
- wordpress - 上传完成时媒体库“HTTP 错误”
- c# - Xceed DocX 库与 .Net Core 2.0 一起使用时会创建空白 .docx 文件
- r - (错误)在`xts`中理解`first`
- sql - 连接多个表/约束或 has_one_through 与辅助约束