C# Blazor Cascadingparameter - Component Update

Asked

Viewed 43 times

2

Good afternoon,

I’m starting in the blazor c# I’m trying to make a contextmenu component that should be instantiated in my Mainlayout, where every child that is rendered in Mainlayout @body has access to that contextmenu and can pass a renderFragment as content from that menu.

The code I’ve made so far was:

Mainlayout:

@inherits LayoutComponentBase

<div class="sidebar" @onclick="sideBar_Click">
    <NavMenu />
</div>

<div class="main">
    <div class="content px-4">
        <CascadingValue Value="contextMenu">
            @Body
        </CascadingValue>
    </div>
</div>


@code {
    ContextMenu contextMenu { get; set; }

    protected override void OnInitialized()
    {
        if (contextMenu == null)
            contextMenu = new ContextMenu();
        base.OnInitialized();
    }

    private void sideBar_Click()
    {
        contextMenu.Teste();
    }
}


My Contextmenu component:

<div style="background-color:cornflowerblue;">
    <div>
        @Conteudo
        <span>Conteudo dinamico: @dinamico</span>
    </div>
</div>

@code {
    [Parameter] public RenderFragment Conteudo { get; set; }
    private string dinamico = "";

    public void Teste()
    {
        Console.WriteLine("Teste executado");
        dinamico = "texto dinamico";
        Console.WriteLine(dinamico);
        StateHasChanged();
    }
}

My Index, which is being rendered in Body:

@page "/"

<ContextMenu @ref="contextMenu">
    <Conteudo>
        <span>Teste</span>
    </Conteudo>
</ContextMenu>

@code{
    [CascadingParameter] ContextMenu contextMenu { get; set; }
}

That is, at the end of everything I want to click on the div sideBard, to be fired an event in my Contexmenu that has its contents defined in each child component , and the contextmenu component is updated.

The way I’m doing presents the following error in the browser console, when I click on the sidebard:

Unhandled Exception Rendering Component: The render Handle is not yet Assigned.

Does anyone know if I’m doing this component upgrade right? I searched a lot but found nothing mixed cascade parameters with renderfragments.

If possible someone help solve the problem ae or show another viable solution to the problem I thank you,

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.