[英]How can I create a List of Razor Page in Blazor
I'm trying to create a list of razor page.我正在尝试创建一个剃刀页面列表。
I explain myself, in my "Index.razor", I actually have that :我解释自己,在我的“Index.razor”中,我实际上有:
@page "/"
@inject IJSRuntime JSRuntime
<h1 style="text-align:center;color:red" >Hey :)</h1>
<br />
<Counter/>
<NewCounter/>
<AnotherVersion/>
Who redirect to Counter.razor, NewCounter.razor & AnotherVersion.razor.谁重定向到 Counter.razor、NewCounter.razor 和 AnotherVersion.razor。
but, like a string list I want to create something like that : having a list like that :但是,就像一个字符串列表一样,我想创建类似的东西:有一个这样的列表:
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 "/"
@foreach (string pageName in pageNames)
{
<ul>
<li><a href="@pageName">@pageName</a></li>
</ul>
}
@code {
private List<string> pageNames = new List<string>
{
"Counter",
"NewCounter",
"AnotherVersion"
};
}
You can use the DynamicComponent
to render components from a type.您可以使用DynamicComponent
来呈现类型中的组件。
@foreach (var component in new[]{ typeof(SurveyPrompt), typeof(Counter), typeof(NewCounter) })
{
<DynamicComponent Type=@component />
}
Okay someone post the answer and delete it right after.好的,有人发布答案并在之后立即将其删除。
So, here the answer :所以,这里的答案:
@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) };
}
(If he repost his message, I will delete mine) (如果他转发他的消息,我会删除我的)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.