You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to achieve the next in Blazor WebAssembly; I need to be able to listen for an event in a component, and use both Javscript and C# code like the next way:
<button onclick="console.log('Showing event from Js: ' + event); @Test()">Test</button>
@code {
public void Test()
{
JSRuntime.InvokeVoidAsync("console.log", "Showing log from C#");
}
}
The first problem is that I get the error "cannot implicitly convert type void to object", so if I change the Test method signature just to return an object
public object Test()
{
JSRuntime.InvokeVoidAsync("console.log", "Showing log from C#");
return null;
}
The App compiles but once the page is loaded, the "Test()" function is executed automatically, and if I click the button, it just executes the Javascript code, and not both pieces of code.
I know I should handle events in Blazor prefixing the event name with the "@" symbol in order to call a C# method, and execute Javascript code there using the interop, but in that way, I can't use the default Javascript "event" which I need to use, instead of the Blazor "event" version.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm trying to achieve the next in Blazor WebAssembly; I need to be able to listen for an event in a component, and use both Javscript and C# code like the next way:
The first problem is that I get the error "cannot implicitly convert type void to object", so if I change the Test method signature just to return an object
The App compiles but once the page is loaded, the "Test()" function is executed automatically, and if I click the button, it just executes the Javascript code, and not both pieces of code.
I know I should handle events in Blazor prefixing the event name with the "@" symbol in order to call a C# method, and execute Javascript code there using the interop, but in that way, I can't use the default Javascript "event" which I need to use, instead of the Blazor "event" version.
Thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions