Skip to main content

GoLive.Generator.BlazorInterop by surgicalcoder

Nuget / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

info

Name: GoLive.Generator.BlazorInterop

Generates strongly typed methods that interop into Javascript.

Author: surgicalcoder

NuGet: https://www.nuget.org/packages/GoLive.Generator.BlazorInterop/

You can find more details at https://github.com/surgicalcoder/BlazorInteropGenerator

Source : https://github.com/surgicalcoder/BlazorInteropGenerator

Original Readme

note

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 of 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 of T . Placeholders {0} and {1} are replaced with the function name and arguments, respectively.

About

note

Generating interop from C# to javascript for Blazor

How to use

Example ( source csproj, source files )

This is the CSharp Project that references GoLive.Generator.BlazorInterop

<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="GoLive.Generator.BlazorInterop" Version="2.0.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.0-rc.2.24474.3" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="9.0.0-rc.2.24474.3" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="BlazorInterop.json" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>


</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

using System.Threading.Tasks;
using Microsoft.JSInterop;

namespace GoLive.Generator.BlazorInterop.Playground.Client
{
public static class JSInterop
{
public static string _window_blazorInterop_showModal => "window.blazorInterop.showModal";

public static async Task showModalVoidAsync(this IJSRuntime JSRuntime, object @dialogId)
{
await JSRuntime.InvokeVoidAsync("window.blazorInterop.showModal", @dialogId);
}

public static async Task<T> showModalAsync<T>(this IJSRuntime JSRuntime, object @dialogId)
{
return await JSRuntime.InvokeAsync<T>("window.blazorInterop.showModal", @dialogId);
}

public static string _window_blazorInterop_setPageTitle => "window.blazorInterop.setPageTitle";

public static async Task setPageTitleVoidAsync(this IJSRuntime JSRuntime, object @title)
{
await JSRuntime.InvokeVoidAsync("window.blazorInterop.setPageTitle", @title);
}

public static async Task<T> setPageTitleAsync<T>(this IJSRuntime JSRuntime, object @title)
{
return await JSRuntime.InvokeAsync<T>("window.blazorInterop.setPageTitle", @title);
}
}
}

Usefull

Download Example (.NET C# )

Share GoLive.Generator.BlazorInterop

https://ignatandrei.github.io/RSCG_Examples/v2/docs/GoLive.Generator.BlazorInterop

In the same category (Blazor) - 1 other generators

Blazorators