你需要深入挖掘,而且有点复杂。
-
AuthorizeView 继承自 AuthorizeViewCore
-
AuthorizedRouteView 构建它自己的 AuthorizeRouteViewCore 继承自 AuthorizeViewCore。
AuthorizedRouteView底部的代码:
private sealed class AuthorizeRouteViewCore : AuthorizeViewCore
{
[Parameter]
public RouteData RouteData { get; set; } = default!;
protected override IAuthorizeData[]? GetAuthorizeData()
=> AttributeAuthorizeDataCache.GetAuthorizeDataForType(RouteData.PageType);
}
AuthorizedRouteView 将任何级联捕获到ExistingCascadedAuthenticationState。如果存在一个(不为空),则CascadingAuthenticationState 存在于App 中,因此无需再做任何事情。如果它为空,那么它将CascadingAuthenticationState 作为组件根组件添加到其渲染片段中。这保证了Task<AuthenticationState> 是级联的。
AuthorizeViewCore 捕获级联值:
[CascadingParameter] private Task<AuthenticationState>? AuthenticationState { get; set; }
它在OnParametersSetAsync 中“解包”
currentAuthenticationState = await AuthenticationState;
isAuthorized = await IsAuthorizedAsync(currentAuthenticationState.User);
并在BuildRenderTree 中用于您看到的“上下文”。
var authorized = Authorized ?? ChildContent;
builder.AddContent(0, authorized?.Invoke(currentAuthenticationState!));
内容来自:
RenderFragment<TValue>
声明如下,其中TValue-content-声明为AuthenticationState:
[Parameter] public RenderFragment<AuthenticationState>? Authorized { get; set; }