Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

406
Views
Elemento contenedor no encontrado en el lado del servidor Blazor

Estoy tratando de incrustar un reproductor de audio usando una biblioteca de javascript llamada Wavesurfer.js en mi componente Blazor. Estoy configurando los ID de los divs necesarios generando GUID para ellos en la página razor.cs y luego entregando esos valores al componente .razor :

 // WavePlayer.razor.cs private Guid _mainDivGuid; protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { _interop = new WavesurferJsInterop(_js, DotNetObjectReference.Create<WavePlayer>(this)); if (_interop != null) { await _interop.Create(_mainDivGuid, Options); //preload file if URL already supplied if (Url != null) { await Load(Url); } } } } protected override void OnInitialized() { _mainDivGuid = Guid.NewGuid(); } // WavePlayer.razor <div class="wavesurfer-ui-main" id="@_mainDivGuid"></div>

En la interoperabilidad js, llama al wavesurfer.js real donde se invoca el método Create y pasa el GUID del div donde se debe representar el reproductor.

Cuando ejecuto el componente, aparece un error Error: Container element not found y puedo confirmar esto en js agregando una prueba para

 if (!document.getElementById(mainDivGuid)) { console.log(mainDivGuid); console.log("No div element found for wavesurfer mainDivGuid"); // this gets logged }

mainDivGuid se está creando, es el GUID correcto, pero cuando se invoca el método Create , aparentemente el div no existe en el DOM.

¿No debería usar el método OnAfterRenderAsync para garantizar que el DOM se represente por completo antes de que se llame a Create ? ¡No estoy seguro de cómo el reproductor de olas puede intentar activar sus propios eventos antes de que exista el contenedor!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Debe utilizar una ElementReference cuando trabaje entre código JS y .NET. Algo como a continuación:

 // WavePlayer.razor @inject IJSRuntime Js <div class="wavesurfer-ui-main" ref="@_mainDiv"></div> @code { public ElementReference _mainDiv; protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { await Js.InvokeVoidAsync("Create", _mainDiv); } } }

En JS, el argumento de su función será el objeto DOM.

 function Create(thisIsTheDomObjectHere) { thisIsTheDomObjectHere.innerHTML("Hi there"); }

Ver más aquí:

https://blazor-university.com/javascript-interop/calling-javascript-from-dotnet/passing-html-element-references/

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!