How to pass state change from MainLayout to Body

In my MainLayout I can trigger an operation that will change variable X. This variable is in a service class that is scoped to the current session.

Im my RadzenBody element the current page will be shown. Some of these pages are binding to the same variable X of the scoped service class.

The problem is that StateChanged in MainLayout will update all references to X in MainLayout. But it will not update the page in RadzenBody referencing X. The change of global variable X is not published to the body page. It would be necessary to do something like StateChanged in the body page as well to reflect the new value of the variable.

Do you have any ideas how the event could be propagated?

You can use event. Check for example security service in Blazor WebAssembly app:

1 Like

Thanks! This was easy to implement:

Scoped Service (create additional property):
-public event Action OnSomethingHasHappened;

Sub page:
-OnInitialized: service.OnSomethingHasHappened += MyReaction;
-private Action MyReaction() { ... StateHasChanged(); }

Mainlayout:
-private void SomethingHasChanged() => OnSomethingHasChanged?.Invoke();
-button will trigger { ... SomethingHasChanged() }
=> Sub page will receive event and update via StateHasChanged

Hi. I'm trying to do the opposite. I'm trying affect a StateHasChange trigger in the MainLayout from a sub page. Do you have an example of this? Thank you

As far as we know this is not possible out of the box.