简体   繁体   English

如何在 Blazor 中创建 Razor 页面列表

[英]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.

相关问题 如何将“查询参数”从 a.razor 页面传递到 ASP.NET 核心 Blazor 中的 .cshtml 页面 - How can I pass 'Query Params' from a .razor page to .cshtml page in ASP.NET Core Blazor 如何从 Blazor WebAssembly 中的 Razor Page 访问 body 标签或其他标签? - How can I reach body tag or another tag from Razor Page in Blazor WebAssembly? 如何在运行时加载 Blazor 页面? - How can I load a Blazor page at runtime? 如何创建 blazor 组件? - How can i create a blazor component? 在 Blazor 中,如何使用 EventCallback 创建接口? - In Blazor, how can I create an interface with EventCallback? 我无法在 blazor 服务器应用程序中的 .razor 页面中使用 C# 类 - I can not use a C# class in a .razor page, in a blazor server application Blazor WASM 托管如何在客户端页面中列出所有身份用户? - Blazor WASM hosted how can I list all Identity Users in a Client page? 如何在 Blazor Serv 中执行与 razor 页面输入字段的 onchange 事件相关的 function。 应用程序? (我必须在函数中使用输入值) - How can I perform a function related to a razor page input field's onchange event in Blazor Serv. App? (I have to use the input value in the function) 如何在 Blazor Server 应用程序的 Razor 页面中通过标记代码(单击输入字段时)清空文本输入字段? - How can I empty a text input field via markup code (when I click the Input-field) in my Razor page of my Blazor Server app? 我的 Blazor 服务器应用程序剃须刀页面中有一个 iframe,其中定义了外部 URL。 如何使用按钮强制刷新(重新加载)此 url? - I have an iframe in my Blazor server app razor page in that an external URL is defined. How can I force to refresh (reload) this url using a button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM