GoLive.Generator.BlazorInterop by surgicalcoder
Nuget / site data
Details
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
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
Generating interop from C# to javascript for Blazor
How to use
Example ( source csproj, source files )
- CSharp Project
- BlazorInterop.json
- blazorinterop.js
- Home.razor
- index.html
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>
This is the use of GoLive.Generator.BlazorInterop in BlazorInterop.json
{
"Files": [
{
"Output_DeleteThis_ToHave_InYourProject": "JSInterop.cs",
"ClassName": "JSInterop",
"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});"
}
This is the use of GoLive.Generator.BlazorInterop in blazorinterop.js
window.blazorInterop = {
showModal: function (dialogId) {
window.alert('see after this the page title'+dialogId);
return true;
},
setPageTitle: function(title) {
document.title = title;
},
};
This is the use of GoLive.Generator.BlazorInterop in Home.razor
@page "/"
@inject IJSRuntime JS
<PageTitle>Home</PageTitle>
<h1>Hello, world!</h1>
Welcome to your new app.
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private async Task IncrementCount()
{
currentCount++;
var res= await JS.showModalAsync<bool>(currentCount);
await JS.setPageTitleVoidAsync($" after {currentCount} the result is " + res);
}
}
This is the use of GoLive.Generator.BlazorInterop in index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>MyTestBlazoe</title>
<base href="/" />
<link rel="stylesheet" href="lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="css/app.css" />
<link rel="icon" type="image/png" href="favicon.png" />
<link href="MyTestBlazoe.styles.css" rel="stylesheet" />
<script src="blazorinterop.js" ></script>
</head>
<body>
<div id="app">
<svg class="loading-progress">
<circle r="40%" cx="50%" cy="50%" />
<circle r="40%" cx="50%" cy="50%" />
</svg>
<div class="loading-progress-text"></div>
</div>
<div id="blazor-error-ui">
An unhandled error has occurred.
<a href="." class="reload">Reload</a>
<span class="dismiss">🗙</span>
</div>
<script src="_framework/blazor.webassembly.js"></script>
</body>
</html>
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- Generated.JSInterop.cs
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