如何从 location/url/route 获取组件类型
How to get component type from location/url/route
场景:
我有一个带有基本路由的 Blazor 服务器端应用程序,导航后我需要检查当前页面是否实现了特定的接口。
例如:
NavigationService.LocationChanged += (sender, args) =>
{
Type componentType = GetComponetFromLocation(args.Location);
if (!componentType.GetInterfaces().Contains(typeof(PageBase)) {
}
}
问题:
如何获取当前或特定 url/location 的组件类型?
不确定您想要实现的目标,但这可能会有所帮助:
App.razor
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<CascadingValue Value="@routeData.PageType" Name="PageType" >
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</CascadingValue>
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
由于页面类型现在作为级联值传递,您可以:
@if(!PageType.IsSubclassOf(typeof(PageBase)))
{
<div> ... </div>
}
@if(PageType.GetInterface("PageBase") == null)
{
<div> ... </div>
}
@code {
[CascadingParameter(Name="PageType")]
public Type PageType { get; set; }
}
我在这里使用了两个 @If
块,因为您的问题是关于接口的,但是您的示例似乎是关于基本类型的。其中一个模块应该可以满足您的需求。
您可以在 MainLayout 组件中添加 OnParametersSet 方法...
另加:@using System.Reflection;
protected override void OnParametersSet()
{
// Get the Component Type from the route data
var component = (this.Body.Target as RouteView)?.RouteData.PageType;
// Get a list of all the interfaces implemented by the component.
// It should be: IComponent, IHandleEvent, IHandleAfterRender,
// Unless your routable component has derived from ComponentBase,
// and added interface implementations of its own
var allInterfaces = component.GetInterfaces();
}
场景:
我有一个带有基本路由的 Blazor 服务器端应用程序,导航后我需要检查当前页面是否实现了特定的接口。 例如:
NavigationService.LocationChanged += (sender, args) =>
{
Type componentType = GetComponetFromLocation(args.Location);
if (!componentType.GetInterfaces().Contains(typeof(PageBase)) {
}
}
问题:
如何获取当前或特定 url/location 的组件类型?
不确定您想要实现的目标,但这可能会有所帮助:
App.razor
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<CascadingValue Value="@routeData.PageType" Name="PageType" >
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</CascadingValue>
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
由于页面类型现在作为级联值传递,您可以:
@if(!PageType.IsSubclassOf(typeof(PageBase)))
{
<div> ... </div>
}
@if(PageType.GetInterface("PageBase") == null)
{
<div> ... </div>
}
@code {
[CascadingParameter(Name="PageType")]
public Type PageType { get; set; }
}
我在这里使用了两个 @If
块,因为您的问题是关于接口的,但是您的示例似乎是关于基本类型的。其中一个模块应该可以满足您的需求。
您可以在 MainLayout 组件中添加 OnParametersSet 方法...
另加:@using System.Reflection;
protected override void OnParametersSet()
{
// Get the Component Type from the route data
var component = (this.Body.Target as RouteView)?.RouteData.PageType;
// Get a list of all the interfaces implemented by the component.
// It should be: IComponent, IHandleEvent, IHandleAfterRender,
// Unless your routable component has derived from ComponentBase,
// and added interface implementations of its own
var allInterfaces = component.GetInterfaces();
}