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

Why does my EventCallback loose its Delegate between OnParametersSet and OnAfterRenderAsync?

$
0
0

I am trying to render a Blazor component within a MVC view and have that component re-render when some other property of the MVC model changes.

The Blazor component includes the following:

@code {  private DotNetObjectReference<ProductInstanceFeatures>? _objRef;  [Parameter] public EventCallback<DotNetObjectReference<ProductInstanceFeatures>> Initialized { get; set; }  protected override Task OnAfterRenderAsync(bool firstRender)  {    if (_objRef == null && Initialized.HasDelegate)    {      _objRef = DotNetObjectReference.Create(this);      return Initialized.InvokeAsync(_objRef);    }    return Task.CompletedTask;  }  public void Dispose() => _objRef?.Dispose();  [JSInvokable]  public void Rerender() => StateHasChanged();}

Which is being called from an MVC view:

<component type="typeof(ProductInstanceFeatures)"                 render-mode="ServerPrerendered"                 param-Initialized="@(EventCallback.Factory.Create<DotNetObjectReference<ProductInstanceFeatures>>(this, objRef => Js.InvokeVoidAsync("productFeaturesInitialized", objRef).AsTask()))"/>

Where productFeaturesInitialized is:

  function productFeaturesInitialized(objRef) {    $("#product-type").on("change", function () {      objRef.invokeMethod('Rerender');    });  }

As a side note, I think I would prefer WebAssemblyPrerendered but it seems like CSR is a whole palaver that I don't have the time for right now. Perhaps I have to go down that route? My understanding from https://learn.microsoft.com/en-us/aspnet/core/blazor/components/render-modes?view=aspnetcore-8.0 is that interactivity is what is required. It is also quite confusing that the render-mode on <component> is so different to @rendermode within a Blazor component.

What I am most confused about is that during OnAfterRenderAsync the EventCallback for Initialized never has a Delegate. I tried adding OnParametersSet and every time that is called it does have a Delegate but then after that OnAfterRenderAsync is called and the delegate is gone. I'm assuming there must be something about the lifecycle that I do not understand but I have no idea what that is. Why is this happening and how can I fix it?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



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