[英]How do I use Javascript API in non-Javascript project?
I am working on a C# project which requires some map task. 我正在一个C#项目中,它需要一些地图任务。
What I am doing is writing a html file in C# and display it with a webview-like component. 我正在做的是用C#编写一个html文件,并使用类似webview的组件显示它。
And this is awkward. 这很尴尬。
Somebody came up with a more reasonable way? 有人想出了一种更合理的方法? This can be a general problem since there are so many Javascript API.
这可能是一个普遍的问题,因为有太多的Javascript API。
You would need to have a Javascript engine running within your C program in order to interpret and execute Javascript code. 您需要在C程序中运行Java引擎,才能解释和执行Java代码。 This can be done though it's seldom without issue since a lot of Javascript code assumes the existence of a browser (eg calling
window.location
or methods that add DOM elements). 尽管很少有问题,但是可以做到这一点,因为许多Javascript代码都假定存在浏览器(例如,调用
window.location
或添加DOM元素的方法)。
This is definitely going to be more trouble than it's worth for your particular case - as well as being orders of magnitude less efficient than just writing the algorithm in C#. 对于您的特定情况,这肯定比它值得的麻烦更多,并且比仅用C#编写算法要低几个数量级。 You should be looking for a C# library that simplifies the creation of HTML, rather than trying to embed an interpreted, weakly-typed language.
您应该在寻找一个简化HTML创建的C#库,而不是尝试嵌入一种解释性弱类型的语言。
(However it can be a valid approach for other situations where you have important business logic in Javascript, and need to replicate this in a performance-insensitive batch process. Or if you want to run automated tests on your JS code alongside C#/Java classes.) (但是,对于在Java中具有重要业务逻辑并需要在对性能不敏感的批处理过程中进行复制的其他情况,这可能是一种有效的方法。或者,如果您希望在JS代码上与C#/ Java类一起运行自动化测试, )
An easy way to do it though would be to run a node js server and simply talk over local ports. 不过,一种简单的方法是运行节点js服务器并仅通过本地端口进行通信。 I am sure you would have no trouble talking over http with c# and it will no doubt be much cleaner then the way you are doing it now.
我敢肯定,使用c#通过http进行通讯毫无问题,并且无疑比现在的方法更简洁。
Also you can include the v8 engine straight into you current project. 您也可以将v8引擎直接包含在当前项目中。 It is just a javascript interpreter in C that builds as your usual library header deal ( I can not say how hard this is I have no C# experience ).
它只是C语言中的一个javascript解释器,可以像平常的库头协议那样进行构建(我不能说我没有C#经验这有多难)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.