To improve performance in Blazor JS Interop calls, synchronous and unmarshalled apis are available
I struggle to find more information regarding the unmarshalled.
For example:
//javascript
JSFunctions.f1 = function (fields) {
var f1 = Blazor.platform.readStringField(fields, 0);
var f2 = Blazor.platform.readStringField(fields, 4);
var f3 = Blazor.platform.readStringField(fields, 8);
};
//F#
[<Inject>]
member val Js: IJSRuntime
let js = this.Js :?> IJSUnmarshalledRuntime
js.InvokeUnmarshalled<ElementReference, string, unit>("JSFunctions.f1", er, txt)
- Where are the functions Blazor.platform.* defined?
- Which one should be used to retrieve an argument of type ElementReference?
- What is the second int parameter of the function readStringField and how should it be used?
Blazorobject is a JavaScript object in global namespace (window) which added byblazor.webassembly.jsin Blazor WebAssembly orblazor.server.jsin Blazor Server. It added at the default template of Blazor Apps at the end of index.html (or _Host.cshtml) by following script:The
Blazorobject provideBlazor.platformand some useful api for examplestart()for manual start of Blazor. This script also add another object namedBINDINGto global namespace (window.BINDINGor simplyBINDING). They provide methods for working with .NET objects directly from JavaScript in unmarshalled way. for example you can read different type of data usingYou can read objects like
ElementReferencetype object using:Here fields is the input (struct or class) passed to JS function when you invoke it and index is its field offset in struct/class definition. You can read more about field offset in Microsoft documentations here. Here is some example:
You can also return a .Net object directly from JavaScript functions using
BINDINGas below:You can discover more about these two objects in your browser developer tool for example by writing
Blazor.platform.orBINDING.: