4

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.

9
  • I don't see where you are inheriting from ComponentBase. Perhaps you forgot to have BaseAdminComponent do that? Commented Jun 24, 2022 at 18:22
  • Have you included @inherits BaseAdminComponent in your FacilitiesView.razor file? Commented Jun 24, 2022 at 18:37
  • oh, sorry, yes, BaseAdminComponent should inherit from ComponentBase Commented Jun 24, 2022 at 18:42
  • @AstridE. I have not included any of that in the razor file Commented Jun 24, 2022 at 18:47
  • 1
    Okay, I went back and tried it again, and it does actually work, I was just missing a couple of things, if you make that an answer, I will accept it Commented Jun 24, 2022 at 19:21

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

4 Comments

I found it so strange that I have to both inherit in the class file, as well as in the razor file, that seems so strange
It is not strange at all. The base component is split into a razor and cs file. So you have to inherit from both as well.
+1. I believe it's because the class is declared partial, so both "parts" (the .razor/.cs files) must inherit from the same base.
@EvilDr I like your theory. I have tried to find documentation to support this, but to no avail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.