¡Trabajando en una aplicación blazor, tiene un método que debe llamarse desde JS! Cuando intento llamar al método sin la palabra clave estática, aparece este error:
The assembly 'AssemblyName' does not contain a public invokable method with [JSInvokableAttribute("INIT_COMPLIANCE")].Tan pronto como agrego estática, lo reconoce y lo carga bien.
Aquí está mi código en el componente blazor:
protected override Task OnInitializedAsync() { jsToDotNetBridgeReference = DotNetObjectReference.Create(this); js.InvokeVoidAsync("SetDotNetReference", jsToDotNetBridgeReference); return base.OnInitializedAsync(); } [JSInvokableAttribute("INIT_COMPLIANCE")] public async void InitComplianceComponent(string token) { id_token = token; Console.WriteLine(token); await GetData(); }JS-
self.addNewCompliance = function () { const url = someurl; var resource = window.authContext.getResourceForEndpoint(url); window.authContext.acquireToken(resource, function (error, token) { // Handle ADAL Error if (error || !token) { console.log('ADAL Error Occurred: ' + error); return; } else { Blazor.start().then(() => { window.InitComplianceComponent(window.DotNet, token); }) //window.InitComplianceComponent(window.DotNet, token); } }); }BlazorInterop.js
InitComplianceComponent = function (dotnetObj, token) { dotnetObj.invokeMethod("AssemblyName", "INIT_COMPLIANCE", token); } No quiero usar la palabra clave estática ya que GetData es un método no estático y no puedo llamarlo desde el método estático.
¿Dónde me equivoco aquí?
puedes hacer esto:
protected override async Task OnInitializedAsync() { await JSRuntime.InvokeVoidAsync("yourJsFunctionName", DotNetObjectReference.Create(this), nameof(MethodToInvokeFromJs)); } [JSInvokable] public void MethodToInvokeFromJs() { //your code StateHasChanged(); }y su código js se verá así:
function yourJsFunctionName(instance, callback) { instance.invokeMethodAsync(callback); }