BlazorNativeJs is experimental project aiming to support direct access to DOM/JS objects (e.g. window,document,events) from C# code in Blazor WASM application.
- Add Nuget package https://www.nuget.org/packages/BlazorNativeJs.Server/ to your Blazor.Server project.
- Add
app.UseNativeJsHack();
in your Blazor server app (see \Samples\01\BlazorApp.Server\Startup.cs). This allows BlazorNativeJs to handle event objects and also provide necessary JS code. - Add Nuget package https://www.nuget.org/packages/BlazorNativeJs/ to your Blazor.Client project.
- Call
NativeJs.Initialize(...)
from Program.cs (see \Samples\01\BlazorApp.Client\Program.cs). - Sample usage:
NativeJs.GetWindow().document.getElementsByTagName("h1")[0].innerText="BlazorNativeJs is great!";
- See \Samples\01\BlazorApp.Client\Pages for further usage examples.
Every object to be returned from JS area is stored to a specific dictionary in browser and a referencing key is returned to C# area instead. C# instantiates a Dynamic object keeping the reference. Accessing a member of the Dynamic object causes call a specific JS function which translates the reference back to the real JS object and process the requested action.
Some expressions might cause many roundtrips (e.g. NativeJs.GetWindow().document.getElementsByTagName("h1")[0]) between C# and JS. Using Expressions signicicantly lowers that as it only notes the path and evaluates all at once on JS.
This project is experimental and in early development phase. It's expected such case will happen. Don't hesitate to create issue with minimal repro steps.
In order to make all this functional, it's needed to inject some specific JS code (see \src\BlazorNativeJs.Server\NativeJsApplicationBuilderExtensions.cs) into blazor.webassembly.js. I haven't found a way to do it during Blazor framework initialization nor page load so it's done on server side. If anybody success on this field, don't hesitate to make a PR.
- all is provided as is without any warranty.
- the target of this concept has been "make it functional for any price". Therefore some pieces are bit "hacky".
- developed with version 3.2.0-preview1.20073.1 wasm.