简体   繁体   English

在.NET桌面和浏览器之间共享JavaScript代码

[英]Sharing JavaScript code between .NET desktop and browser

I have a set of core, complicated JavaScript data structures/classes that I'd like to be able to use both in the browser as JavaScript and run on the desktop with .NET 3.5. 我有一组核心的,复杂的JavaScript数据结构/类,我希望既可以在浏览器中将它们用作JavaScript,又可以在具有.NET 3.5的桌面上运行。 Is it possible to compile web-friendly JavaScript into assemblies that my C# code can access? 是否可以将网络友好的JavaScript编译为我的C#代码可以访问的程序集?

  • Managed JScript - Is there a compiler for this available that will produce something that can run on the desktop CLR? 托管JScript-是否有可用的编译器来生成可以在桌面CLR上运行的程序? I've only seen released examples for the Silverlight runtime. 我只看到了Silverlight运行时的已发布示例。 It doesn't sound like it's officially available on the desktop but I'm willing to try something less blessed, like can I compile with the Silverlight tools and then run on the desktop CLR? 听起来好像它不能在桌面上正式可用,但我愿意尝试一些不太受人欢迎的事情,例如我可以使用Silverlight工具进行编译然后在桌面CLR上运行吗?
  • JScript .NET - Sounds like it's got enough custom language extensions where having the same code run in a browser would be really hard, but maybe there's a way...? JScript .NET-听起来好像有足够的自定义语言扩展,要在浏览器中运行相同的代码确实很困难,但是也许有办法...?

If neither of those work, I guess my options are to: 如果这些都不起作用,我想我的选择是:

  • Have a C# version and a JavaScript version which I have to keep in sync (yuck). 有一个C#版本和一个JavaScript版本,我必须保持同步(糟糕)。
  • Write a preprocessor of sorts that runs at compile time to turn my JavaScript into JScript .NET or something that I can compile into .NET assemblies. 编写一种在编译时运行的预处理器,以将我的JavaScript转换为JScript .NET或可以编译为.NET程序集的某种预处理器。
  • Script#: Looks like this turns C# code into JavaScript code. 脚本编号:看起来像这样将C#代码转换为JavaScript代码。 Not awesome since I'd prefer to be able to heavily tune JavaScript code directly, but it could work. 不太好,因为我希望能够直接大量调整JavaScript代码,但是它可以工作。

Anyone have success with any of these options? 任何人都能成功使用这些选项吗?

JScript.Net is actually mostly EMCAScript3 compliant. JScript.Net实际上主要符合EMCAScript3。 As long as you treat your code as a processing library (only have it do crunching, etc - use callbacks to interact with the program), you should be fine. 只要将代码视为处理库(仅对代码进行处理等-使用回调与程序进行交互),就可以了。 I'm doing something similar, using javascript as my core parsing library, and then using it within python, dotnet, and php. 我正在做类似的事情,使用javascript作为我的核心解析库,然后在python,dotnet和php中使用它。

Managed JavaScript (IronJScript) appears to have been killed. 托管JavaScript(IronJScript)似乎已被杀死。 See this answer : 看到这个答案

How about another option: 另一个选择如何:

  • Host the javascript application inside your desktop app via winforms' WebBrowser control? 通过winforms的WebBrowser控件将javascript应用程序托管在桌面应用程序中?

Is this possible? 这可能吗?

You can also access the DOM from WebBrowser control but not everything you want is fully supported. 您还可以从WebBrowser控件访问DOM,但并非完全支持所需的所有内容。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM