Skip to content

surgicalcoder/BlazorInteropGenerator

Repository files navigation

BlazorInteropGenerator

Generates Blazor -> Javascript strongly typed interop methods, by parsing the Javascript it self and generating extension methods for IJSRuntime.

Usage

Firstly, add the project from Nuget - GoLive.Generator.BlazorInterop, then add an AdditionalFile in your .csproj named "BlazorInterop.json", like so:

<ItemGroup>
     <AdditionalFiles Include="BlazorInterop.json" />
</ItemGroup>

Once that's done, add the settings file and change as required:

{
  "Files": [
    {
      "Output": "JSInterop.cs",
      "Source": "wwwroot\\blazorinterop.js",
      "Namespace": "GoLive.Generator.BlazorInterop.Playground.Client",
      "ObjectToInterop": "window.blazorInterop",
      "Init": ["window={}"]
    }
  ],
  "InvokeVoidString": "await JSRuntime.InvokeVoidAsync(\"{0}\", {1});",
  "InvokeString": "return await JSRuntime.InvokeAsync<T>(\"{0}\",{1});"
}

Description of Each Option

  • Files: An array of file objects specifying details of the files involved in the interop process.
    • Output: The name of the output C# file to be generated.
    • Source: The path to the source JavaScript file used for the interop.
    • Namespace: The namespace used in the generated C# file.
    • ObjectToInterop: The JavaScript object used for the interop.
    • Init: An array of initialization scripts executed before the interop. In this example above, we are interop'ing to window.blazorInterop, and window doesn't exist, so we have to create it.
  • InvokeVoidString: A template string for invoking a JavaScript function that does not return a value using JSRuntime.InvokeVoidAsync. Placeholders {0} and {1} are replaced with the function name and arguments, respectively.
  • InvokeString: A template string for invoking a JavaScript function that returns a value using JSRuntime.InvokeAsync. Placeholders {0} and {1} are replaced with the function name and arguments, respectively.

About

Generates Blazor -> Javascript strongly typed interop methods.

Resources

License

Stars

Watchers

Forks

Packages

No packages published
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy