Blazor.Diagrams icon indicating copy to clipboard operation
Blazor.Diagrams copied to clipboard

DiagramChanged event being accessed before OnParametersSet

Open peterharding93 opened this issue 5 months ago • 1 comments

I'm hitting a null reference exception when trying to use the diagram. The bug seems pretty simple inside DiagramCanvas.razor.cs:

protected override void OnInitialized()
{
	base.OnInitialized();
	_reference = DotNetObjectReference.Create(this);
	BlazorDiagram.Changed += OnDiagramChanged;   // <-- Faulty 
}

The root cause is that the [CascadingParameter] BlazorDiagram is not guaranteed to be set until OnAfterParametersSet(). I'm a little surprised this has worked at all. The blazor documentation is pretty clear that this shouldn't be done and is unlikely to work correctly. The entire purpose of OnAfterParametersSet is to handle thi.

I've tried a lot of hacks to 'trick' the renderer to setting the parameters before initializing the component, but can't get it to work. I am about to branch the source code to fix up the binding myself.

I am using .NET 9 with InteractiveServer mode with prerendering disabled. I actually have two projects - one works and the other doesn't and I cannot work out the difference. The working one was originally a .NET 6 project and has been updated with each release. The broken project is new from scratch using the new blazor web app.

I noticed that in my working project (which has worked well for years) I did have to set the cascading paremeter to IsFixed="false" - but that doesn't work for the second project.

Edit: Using version: PackageReference Include="Z.Blazor.Diagrams" Version="3.0.3"

peterharding93 avatar May 30 '25 01:05 peterharding93