在我的应用程序中,我计划设置两个布局。BeforeLoginLayout和AfterLoginLayout。目前,在App.Razor中,我将defaultLayout设置为BeforeLoginLayout。我想在用户登录应用程序后更改默认布局。如何在Blazor中实现此操作?我可以在单个页面上进行设置。我想避免这种情况,并将其设置为默认布局。
这里有一些代码和演示页面,展示了如何动态更改布局。您应该能够根据自己的需求进行调整。
按照以下方式修改App:
<Router AppAssembly="@typeof(Program).Assembly" PreferExactMatches="@true">
<Found Context="routeData">
<CascadingValue Value="this">
<RouteView RouteData="@routeData" DefaultLayout="this.LayoutType" />
</CascadingValue>
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
@code {
private Type LayoutType = typeof(MainLayout);
public void SetLayout(Type layout)
{
if (layout != LayoutType)
{
this.LayoutType = layout;
StateHasChanged();
}
}
}
OtherLayout
是 MainLayout
的副本,但有一些不同之处。@page "/"
<div class="m-2 p-2">
<button class="btn btn-secondary" @onclick="() => ButtonClick(typeof(MainLayout))">Main Layout</button>
<button class="btn btn-dark ms-2" @onclick="() => ButtonClick(typeof(OtherLayout))">Other Layout</button>
</div>
@code {
[CascadingParameter] public App MyApp { get; set; }
void ButtonClick(Type layoutType)
{
MyApp.SetLayout(layoutType);
}
}
OtherLayout
吗? - MrC aka Shaun Curtis