Blazor-Diagrams / Blazor.Diagrams

A fully customizable and extensible all-purpose diagrams library for Blazor
https://blazor-diagrams.zhaytam.com
MIT License
919 stars 176 forks source link

Diagram Container Null #291

Closed AlmightyLks closed 1 year ago

AlmightyLks commented 1 year ago

After overhauling a different corner of my program, I came back to see, that my Diagram's Container happens to be null, on first AfterRender

The Diagram object is initialized within the OnInitializedAsync, and within OnAfterRenderAsync I am trying to access the Diagram, that's where the Container gives me null, causing me a NRE.

Do you happen to know under what circumstances a Diagram's Container is null?

Looking into the source, the Container is only ever set through https://github.com/Blazor-Diagrams/Blazor.Diagrams/blob/ef9309c310f26a4828569c26fb8ebefa0278f83b/src/Blazor.Diagrams.Core/Diagram.cs#L331-L339 and from what I recall, I didn't need to give it an explicit Container rectangle for a Diagram to be displayed

zHaytam commented 1 year ago

Hello,

Normally, the DiagramCanvas component will set the Container after the first render (see https://github.com/Blazor-Diagrams/Blazor.Diagrams/blob/master/src/Blazor.Diagrams/Components/DiagramCanvas.razor.cs#L47)

The only reason it might be null is probably because you're trying to access it before it gets the boundaries from JS? Please try using the ContainerChanged event and see if you do get it, if you do, then you're probably just accessing it before it's available, since iirc Blazor's OnAfterRender isn't hierarchical

AlmightyLks commented 1 year ago

Yeah; I am accessing it before it fires that event

Best bet would be adding a time delay

await Task.Delay(500);
// ...

within OnAfterRender, I guess

zHaytam commented 1 year ago

Or use the event, that would be more "natural". But it's up to you!

AlmightyLks commented 1 year ago

Wouldnt the event also fire under several frontend resize circumstances?

zHaytam commented 1 year ago

Indeed! But you can only consider the first one