How to bind native event with document in razor page(Blazor server side application not interop file)
<button @onmousemove="@OnMouseMove">Login</Button>
This is native event bind in button element.
Need bind same native event in Document
How to bind native event with document in razor page(Blazor server side application not interop file)
<button @onmousemove="@OnMouseMove">Login</Button>
This is native event bind in button element.
Need bind same native event in Document
You can achieve it through JavaScript and JavaScript interoperability (JS interop). You need subscribe on event on front-end and call .Net code
document.addEventListener("mousemove", function () {
DotNet.invokeMethodAsync('SoWebApplication (Assembly Name)', 'ReturnDataAsync (Method Name)')
.then(data => {
//This line appears in browser dev console.
console.log(data + " from JS.");
});
});
and create static method for calling. From documentation:
The asynchronous version is preferred to support Blazor Server scenarios. The .NET method must be public, static, and have the [JSInvokable] attribute. Calling open generic methods isn't currently supported.
using System;
using System.Threading.Tasks;
using Microsoft.JSInterop;
static public class Test
{
[JSInvokable]
public static Task<string> ReturnDataAsync()
{
//This line appears in server console.
Console.WriteLine("Hello from server");
return Task.FromResult("Hello");
}
}