C#C
C#3y ago
Camster

❔ Blazor State Container Prompt, Standardized for all Pages

I have a simple state container in blazor. The goal is to provide the AppEntity value:

public class StateContainer {
    public AppEntity App { get; set; }
    public event Action OnStateChange;
    public void SetApp(AppEntity app) {
        App = app;
        NotifyStateChanged();
    }

    private void NotifyStateChanged() => OnStateChange?.Invoke();
}


The container is injected in the services collection.

builder.Services.AddSingleton<StateContainer>();


I have a component which provides the user a selection dropdown to set the AppEntity in the State Container. Subsequent pages inject the State Container and use it if needed.

My question is: if for some reason a page that requires the State Container has a null value for AppEntity (e.g., user navigated to a specific page by url, bypassing the selection page), how can I have a generic popup prompt that prompts the user for the selection? Ideally, I'd want to minimize the code in any page that needs it as much as possible. The only way I can think to do this is to build the prompt in a base component and inherit from it, but perhaps there might be a simpler or more elegant way.
Was this page helpful?