简体   繁体   English

如何在没有 Blazor Web 组件的情况下从 JavaScript 使用 C# WebAssemly

[英]How to use C# WebAssemly from JavaScript without Blazor web components

To compile C# into WebAssemly and interop with JS it's required to use Blazor WebAssembly ASP.NET framework, which is designed for SPA and contains lot of overhead in case you just want to use a C# library from JS.要将 C# 编译成 WebAssemly 并与 JS 进行互操作,需要使用 Blazor WebAssembly ASP.NET 框架,该框架专为 SPA 设计并且包含大量开销,以防您只想使用来自 JS 的 C# 库。

What is the minimum setup to just compile a DLL to WebAssembly and use it from JavaScript?将 DLL 编译为 WebAssembly 并从 JavaScript 使用它的最低设置是什么?

Create a new empty C# project with the following configuration (via .csproj):使用以下配置(通过 .csproj)创建一个新的空 C# 项目:

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

    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <LangVersion>10</LangVersion>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.0" />
        <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.0" PrivateAssets="all" />
    </ItemGroup>

</Project>

Initialize Blazor JS runtime and specify the bindings:初始化 Blazor JS 运行时并指定绑定:

namespace WasmTest;

public class Program
{
    private static IJSRuntime js;

    private static async Task Main (string[] args)
    {
        var builder = WebAssemblyHostBuilder.CreateDefault(args);
        var host = builder.Build();
        js = host.Services.GetRequiredService<IJSRuntime>();
        await host.RunAsync();
    }

    [JSInvokable]
    public static async Task<string> BuildMessage (string name)
    {
        var time = await GetTimeViaJS();
        return $"Hello {name}! Current time is {time}.";
    }

    public static async Task<DateTime> GetTimeViaJS ()
    {
        return await js.InvokeAsync<DateTime>("getTime");
    }
}

Publish with dotnet publish and use the C# library from JS:使用dotnet publish并使用来自 JS 的 C# 库:

<script src="_framework/blazor.webassembly.js" autostart="false"></script>

<script>
    window.getTime = () => {
        return new Date().toJSON();
    };
    window.onload = async function () {
        await Blazor.start();
        const msg = await DotNet.invokeMethodAsync("WasmTest", "BuildMessage", "John");
        console.log(msg);
    };
</script>

Here is an example template solution on GitHub: https://github.com/Elringus/WasmTest以下是 GitHub 上的示例模板解决方案: https : //github.com/Elringus/WasmTest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Blazor Web 程序集中将 c# 变量传递给 javascript - How to Pass a c# variable to javascript in Blazor Web Assembly 如何从 Blazor ZD7EFA19FBE7D3972FD74ZZ 代码调用 static JavaScript 方法 - How to call a static JavaScript method from Blazor C# code 从 JavaScript 渲染 Blazor 组件 - 如何更新 Blazor 组件参数从 Z686155AF75A60A0F6EZDED83 之后的 Z686155AF75A60A0F6EZDED83 组件 - Render Blazor components from JavaScript - How to Update a Blazor component parameter from JavaScript after component render Blazor JS互操作 - 从javascript调用C#实例方法 - Blazor JS interop - calling C# instance methods from javascript 如何在 HTML/JavaScript 应用程序中使用 LWC 作为 web 组件,而无需像节点这样的服务器 - How to use LWC as web components in HTML/JavaScript apps without a server like node 如何在不转换为json的情况下使用C#数组到javascript数组? - How to use C# array to javascript array without converting to json? 如何在C#中使用JavaScript - how to use javascript in c# c# Blazor WASM Javascript 未在调试中显示 - c# Blazor WASM Javascript not showing in debugging C#Winform上的Java Web值 - javascript on web value from c# winform 无法将图像 Base64 字符串从 JavaScript 传递到 Blazor 服务器中的 C# - Cannot pass image Base64 string from JavaScript to C# in Blazor server app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM