Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Blazor 如何从location/url/route获取组件类型 脚本:_Blazor_.net 5_Blazor Routing - Fatal编程技术网

Blazor 如何从location/url/route获取组件类型 脚本:

Blazor 如何从location/url/route获取组件类型 脚本:,blazor,.net-5,blazor-routing,Blazor,.net 5,Blazor Routing,我有一个Blazor服务器端应用程序,带有基本路由,在导航之后,我需要检查当前页面是否实现了特定的接口。 e、 g: 问题: 如何获取当前或特定url/位置的组件类型?不确定您想要实现什么,但这可能会有所帮助: App.razor 由于页面类型现在作为级联值传递,您可以: @if(!PageType.IsSubclassOf(typeof(PageBase))) { <div> ... </div> } @if(PageType.GetInterface(&q

我有一个Blazor服务器端应用程序,带有基本路由,在导航之后,我需要检查当前页面是否实现了特定的接口。 e、 g:

问题:
如何获取当前或特定url/位置的组件类型?

不确定您想要实现什么,但这可能会有所帮助:

App.razor

由于页面类型现在作为级联值传递,您可以:


@if(!PageType.IsSubclassOf(typeof(PageBase)))
{
    <div> ... </div>
}

@if(PageType.GetInterface("PageBase") == null)
{
    <div> ... </div>
}

@code {
    [CascadingParameter(Name="PageType")]
    public Type PageType { get; set; }
}


我在这里使用了两个@If块,因为您的问题涉及接口,但您的示例似乎是关于基本类型的。其中一个模块应该满足您的需要。

不确定您想要实现什么,但这可能会有帮助:

App.razor

由于页面类型现在作为级联值传递,您可以:


@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方法

还添加:@使用System.Reflection


您可以在MainLayout组件中添加OnParametersSet方法

还添加:@使用System.Reflection


@if(!PageType.IsSubclassOf(typeof(PageBase)))
{
    <div> ... </div>
}

@if(PageType.GetInterface("PageBase") == null)
{
    <div> ... </div>
}

@code {
    [CascadingParameter(Name="PageType")]
    public Type PageType { get; set; }
}

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();
    
    
}