Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4011

NullReferenceException when calling StateHasChanged() in base component class in .NET MAUI Blazor Hybrid app

$
0
0

I'm working on a cross-platform project using Blazor WebAssembly and .NET MAUI Blazor Hybrid (targeting .NET 8). I'm using Fluxor for state management across both environments.

In my shared component library, I have a base class called SettingsBase that looks something like this:

public class SettingsBase : ComponentBase{    protected void OnStateChanged(object? sender, EventArgs e)    {        StateHasChanged();    }}

All of my Razor components inherit from SettingsBase. In each component, I register for state change notifications like this:

ApplicationState.StateChanged += OnStateChanged;

The idea is that whenever any relevant state changes, the OnStateChanged method triggers StateHasChanged(), causing the component to re-render — and this works perfectly in Blazor WebAssembly.

However, when I run the same shared components in a .NET MAUI Blazor Hybrid app, I get the following exception:

System.NullReferenceException   at Microsoft.AspNetCore.Components.ComponentBase.StateHasChanged()

It seems like StateHasChanged() is being called before the component is fully initialized or attached to the Blazor rendering system in MAUI.

My questions:Has anyone else encountered this issue when using StateHasChanged() in shared components between Blazor WASM and MAUI?

Does MAUI Blazor Hybrid handle component lifecycle differently in a way that affects StateHasChanged()?

Is there a best practice for subscribing to state changes in shared Razor components that avoids this issue?


Viewing all articles
Browse latest Browse all 4011

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>