Home > Enterprise >  Inheritance With Blazor Component Classes
Inheritance With Blazor Component Classes

Time:06-29

I'm trying to implement a simple heirarchy for the classes behind my blazor pages, but for some reason I keep getting a build error.

What I've implemented is a simple way to update breadcrumbs from mudblazor using inheritance using an interface IBreadCrumbEnabled

namespace BlazorConversionProject.Areas
{
    public interface IBreadCrumbEnabled
    {
        void UpdateBreadcrumbs();
    }
}

I've made a base class called BaseAdminComponent.cs

using Microsoft.AspNetCore.Components;
using MudBlazor;

namespace BlazorConversionProject.Areas
{
    public class BaseAdminComponent : ComponentBase, IBreadCrumbEnabled
    {
        [CascadingParameter]
        public List<BreadcrumbItem> Crumbs { get; set; }

        public void UpdateBreadcrumbs()
        {
            Crumbs.Add(new BreadcrumbItem("Admin", null));
        }
    }
}

Then this is implemented by whatever page I'm using

using Microsoft.AspNetCore.Components;
using MudBlazor;

namespace BlazorConversionProject.Areas.Facilities
{
    public partial class FacilitiesView : BaseAdminComponent
    {
        [Inject]
        public NavigationManager NavManager { get; set; }
    }
}

When I try to build this, I get:

Error   CS0115  'FacilitiesView.BuildRenderTree(RenderTreeBuilder)': no suitable method found to override

If FacilitiesView inherits from ComponentBase directly, or doesn't inherit from anything at all, then the error goes away, but if I try to inherit from any other class in FacilitiesView everything breaks, and I just don't understand why I can't have another class inherit from ComponentBase and then inherit from that for my component.

I checked and can confirm that ComponentBase is not sealed, so it makes no sense why there is no suitable method to override.

CodePudding user response:

When I tried to reproduce this issue, I got an additional error:

Error CS0263 Partial declarations of 'FacilitiesView' must not specify different base classes

The reason this error occurred was that I had only specified the inheritance in FacilitiesView.razor.cs.

I solved it by specifying the inheritance in FacilitiesView.razor as well:

FacilitiesView.razor.cs:

public partial class FacilitiesView : BaseAdminComponent

FacilitiesView.razor:

@inherits BaseAdminComponent
  • Related