electron调用c#动态库
electron调用c#动态库
新建C#动态库
-
方法要以异步任务的方式,可以直接包装,也可以写成天然异步
-
代码如下
public class Class1 { public async Task<Object> Invoke(object input) { return Helper.SayHi("Invoke1:" + (string)input); } public async Task<Object> Invoke2(object input) { return Helper.SayHi("Invoke2:" + (string)input); } static class Helper { public static string SayHi(string param) { return ".NET Welcomes " + param; } }
安装electron-edge-js模块
-
调用代码如下
const edge = require('electron-edge-js'); console.info("call c#") var DemoDll = edge.func({ assemblyFile: "electronedge.dll", typeName: "electronedge.Class1", methodName: "Invoke" }); var DemoDll2 = edge.func({ assemblyFile: "electronedge.dll", typeName: "electronedge.Class1", methodName: "Invoke2" }); // module.exports.DemoDll = DemoDll; module.exports.DemoDll = { demo: DemoDll, demo2:DemoDll2 };
-
node引用如下
const DemoDll = require("./csharputil.js"); DemoDll.DemoDll.demo("test", (err, value)=> { log.debug(value); });
-
页面js引用如下
-
包装如下
function init() { const DemoDll = require("F:/yanghuaihua/electronedge/csharputil.js"); return { demo: DemoDll.DemoDll.demo, demo2:DemoDll.DemoDll.demo2 }; } const initRequire = init();
-
引用如下
<script>window.$ = window.jQuery = require('./js/jquery-3.4.1.min.js');</script> <script type="text/javascript" src="./js/init.js"></script> <script type="text/javascript" src="./js/index.js"></script> <script type="text/javascript"> $("#btn").click(function () { initRequire.demo("test", (err, value) => { $("#demo").append(value); $("#demo").text(value); }); setInterval(()=>{ initRequire.demo2("test", (err, value) => { $("#demo").append(value); $("#demo").text(value); }); }, 1500); }); </script>
-
源码地址
https://github.com/Ants-double/yumi/tree/master/electronedge
作者:Ants_double
出处:https://www.cnblogs.com/ants_double/
本文版权归作者和博客园所有,欢迎转载。转载请在留言板处留言给我,且在文章标明原文链接,谢谢!
如果您觉得本篇博文对您有所收获,觉得我还算用心,请点击右下角的 [大拇指],谢谢!