.Net WebBrowser 中的 HTML 与 WinForms 应用程序之间的双向通信
使用 WebBrowser
控件时,我想将一个对象从 javascript 代码传递到 C#。
解决方案是已知的 - 使用 WebBrowser
的 ObjectForScripting
属性并使用 window.external
从 javascript 代码调用方法。
我知道如何做到这一点,但我想在 javascript 中创建给定类型的对象并将其传递给 C#,而不使用 object
或 dynamic
。示例:
我想在 C# 中从 JS 调用此方法:
public void Test(Foo foo)
{
//do something
}
其中 Foo
是:
[ComVisible(true)]
public class Foo
{
public string Bar;
}
我知道如何调用这些方法:
public void Test2(object foo)
{
//do something
}
public void Test3(dynamic foo)
{
//do something
}
但我想使用第一个 - Test(Foo foo )
我应该怎么做才能将 Foo
类的对象从 JS 传递到 C# 或如何将 JS 对象动态转换为 C# Foo
对象?
When using WebBrowser
control I would like to pass an object from javascript code to C#.
Solution is known - using ObjectForScripting
property of WebBrowser
and invoking methods from javascript code using window.external
.
I know how to do that, but I would like to create object with given type in javascript and pass it to C# without using object
or dynamic
. Example:
I would like to call from JS this method in C#:
public void Test(Foo foo)
{
//do something
}
Where Foo
is:
[ComVisible(true)]
public class Foo
{
public string Bar;
}
I know how to call those methods:
public void Test2(object foo)
{
//do something
}
public void Test3(dynamic foo)
{
//do something
}
but I would like to use the first one - Test(Foo foo)
What should I do to make it possible to pass object of class Foo
from JS to C# or how to cast JS object on the fly to C# Foo
object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
jscript 中的类型系统较弱,因此您无法拥有早期绑定类。我建议您使您的类可自动化并注册项目的类型库以进行后期绑定< /a> 因此可以通过新的 ActiveXObject 在 jscript 中调用它。
The type system is weaker in jscript, so you can't have early binding classes. I suggest you to make your class automatable and register your project's type library for late binding so it can be called in jscript via new ActiveXObject.
我至少找到了两种解决方案。
首先,很明显,这就是盛江的建议。您需要使用这些类型创建一个单独的类库:
必须注册该类型库(项目/属性/构建/注册 COM 互操作)。这些类型,尤其是接口,应该在具有
WebBrowser
控件的 exe 中的方法中使用。例如:现在我们可以在 javascript 中使用这些类型:
不幸的是,必须注册类库,并且您将从 IE 收到ActiveX 警告。
第二个解决方案是不创建类库,而不是将这些类型保留在应用程序exe中。如果我们想将对象从 javascript 传递到 C#,我们仍然需要接口
IFoo
。我们需要在 C# 端为 javascript 创建对象的方法:Javascript 使用:
我们不需要为 COM 互操作注册任何内容,并且不会出现没有 ActiveX 警告 IE。
I found at least two solutions.
First, pretty obvious, is what Sheng Jiang suggested. You need to create a separate class library with those types:
This type library must be registered (Project/Properties/Build/Register for COM interop). Those types, especially interfaces, should be used in methods in exe which has the
WebBrowser
control. For example:Now we can use those types in javascript:
Unfortunately the class library has to be registered and you will get an ActiveX warning from IE.
Second solution is to not create class library, instead of leaving those types in the app exe. We still need interface
IFoo
if we want to pass objects from javascript to C#. And we need methods to create objects on the C# side for javascript:Javascript usage:
We don't need to register anything for COM interop and there will be no ActiveX warning from IE.