skip to Main Content

I do not seem to be able to supply a callback from the parent component to the child, when teh child is rnedered using @Body:

    <CascadingValue  Value="isGB">
        <CascadingValue Value="HandleChange">
            @Body
        </CascadingValue>
    </CascadingValue>
    </div>

@code {
    private void HandleChange(bool isgb)
    {
        throw new Exception();

        isGB = isgb;
        StateHasChanged();
    }
}

What I ave in the child component:

    [CascadingParameter]
    public EventCallback<bool> HandleChange { get; set; }

    private async Task ChangeIsGB(bool isgb)
    {
        IsGB = isgb;
        await HandleChange.InvokeAsync(isgb);
    }

But the event does not fire, sadly enough. Why?

2

Answers


  1. Chosen as BEST ANSWER

    Actually, setting the rendermode in App.razor solved teh issue. I read that without this events would not fire. (Not even click events)

    This is it:

     <Routes @rendermode=RenderMode.InteractiveServer />
    

    This essentially sets the rendermode to interactiev server by default. Otherwise teh default rendermode is static.


  2. You can’t pass the method directly as a CascadingValue the way you’re trying, but what you can do is create an EventCallback and pass that instead.

    Hosting Page (MainLayout.razor?):

    <CascadingValue Value="this.HandleChange">
        @Body
    </CascadingValue>
    
    @code {
        private EventCallback<bool> HandleChange { get; set; }
        private bool IsGB { get; set; }
    
        private override void OnInitializedAsync()
        {
            this.HandleChange = new EventCallback<bool>(this, (Action<bool>)this.HandleChangeEvent);
    
        private void HandleChangeEvent(bool isGB)
        {
            this.IsGb = isGB;
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search