简体   繁体   English

如何在非Javascript项目中使用Javascript API?

[英]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.

相关问题 非Javascript应用程序中的JSON - JSON in non-Javascript applications 非JavaScript版本的asp.net页面的下拉菜单回发? - Drop-down menu postback for non-JavaScript version of asp.net page? 如何确定Office版本号的JavaScript API(适用于Office 2013项目的应用) - How do I determine JavaScript API for Office Version Number (App for Office 2013 Project) 如何在非UITest项目中设置VS以使用ImageComparer? - How do I setup VS to use ImageComparer in a non-UITest project? 如何使用Javascript关键字作为变量名? - How do I use a Javascript keyword as a variable name? 如何将JavaScript库添加到C#MVC项目? - How do I add a JavaScript library to my C# MVC project? ASP.Net-如何包含另一个项目的嵌入式JavaScript文件? - ASP.Net - How do I include an embedded JavaScript file from another project? 如何在javascript和ASP.NET中返回项目的当前域? - How do I return the current domain of my project in javascript and ASP.NET? 如何在ASP.NET Web API项目而不是.NET Core上使用DI - How do I use DI on asp.net web api project not .net core 如何使用Project Oxford的Speech API在WPF应用程序中实现解决方案? - How do I use Project Oxford's Speech API to implement solution into a WPF application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM