问题描述
我想在我的C#程序中托管Javascript。我想允许用户编写自定义Javascript代码,让我的C#程序运行他们的函数,并允许用户使用我的框架代码。
I'd like to host Javascript in my C# program. I would like to allow users to write custom Javascript code, and have my C# program run their functions, as well as allow the users to use my framework code.
是吗有可能做到这一点?如果是这样,怎么做?
Is it possible to do this? If so, how?
编辑:要清楚,我不使用ASP.NET进行此项目。
To be clear, I am not using ASP.NET for this project.
推荐答案
你也可以使用webbrowser控件在html文档中托管javascript,在两者之间进行交互,你可以创建一个COM可见类并设置一个它的实例是ObjectForScripting属性。
you can also use a webbrowser control to host the javascript in a html document, to interact between the two you would make a COM visible class and set an instance of it to the ObjectForScripting property.
现在可以通过javascript中的window.external访问外部类的所有公共成员。此外,从托管代码端,您可以使用文档的InvokeScript方法来调用任何javascript函数。
Any public members of the 'external' class are now accessible through window.external in javascript. Also, from the managed code side you can use the InvokeScript method of the document to call any javascript functions.
这样很容易在两者之间传递复杂对象。
This way it is easy to pass complex objects between the two.
我在Google Earth Api应用程序中使用了这个想法。
I used this idea quite a bit in this Google Earth Api application.http://fraserchapman.blogspot.com/2008/08/google-earth-plug-in-and-c.html
这篇关于如何让C#与Javascript互操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!