blazor アプリケーションで作業すると、JS から呼び出す必要があるメソッドがあります。static キーワードなしでメソッドを呼び出そうとすると、このエラーが発生します-
The assembly 'AssemblyName' does not contain a public invokable method with [JSInvokableAttribute("INIT_COMPLIANCE")].
static を追加するとすぐに、それを認識して正常にロードします。
これが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);
}
GetData
非静的メソッドであるため static キーワードを使用したくなく、静的メソッドから呼び出すことはできません。
ここでどこが間違っていますか。