.Net WebBrowser 中的 HTML 与 WinForms 应用程序之间的双向通信

发布于 2024-09-09 10:02:08 字数 826 浏览 7 评论 0原文

使用 WebBrowser 控件时,我想将一个对象从 javascript 代码传递到 C#。

解决方案是已知的 - 使用 WebBrowserObjectForScripting 属性并使用 window.external 从 javascript 代码调用方法。

我知道如何做到这一点,但我想在 javascript 中创建给定类型的对象并将其传递给 C#,而不使用 objectdynamic。示例:

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯了 2024-09-16 10:02:08

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.

那小子欠揍 2024-09-16 10:02:08

我至少找到了两种解决方案

首先,很明显,这就是盛江的建议。您需要使用这些类型创建一个单独的类库:

[ComVisible(true)]
public interface IFoo
{
    string Bar { get; set; }
}

[ComVisible(true)]
public class Foo : IFoo
{
    public string Bar { get; set; }
}

必须注册该类型库(项目/属性/构建/注册 COM 互操作)。这些类型,尤其是接口,应该在具有 WebBrowser 控件的 exe 中的方法中使用。例如:

public string Bar(IFoo foo)
{
    return foo.Bar;
}

public IFoo Bar2()
{
    return new Foo() { Bar = "asdf" };
}

现在我们可以在 javascript 中使用这些类型:

var foo = new ActiveXObject("ClassLibrary1.Foo");
foo.Bar = "qwer";
alert(external.Bar(x));
var fooFromExternal = external.Bar2();
alert(fooFromExternal);
alert(external.Bar(fooFromExternal));

不幸的是,必须注册类库,并且您将从 IE 收到ActiveX 警告

第二个解决方案是不创建类库,而不是将这些类型保留在应用程序exe中。如果我们想将对象从 javascript 传递到 C#,我们仍然需要接口 IFoo。我们需要在 C# 端为 javascript 创建对象的方法:

public IFoo CreateFoo()
{
    return new Foo() { Bar = "asdf" };
}

public string Bar(IFoo foo)
{
    return foo.Bar;
}

Javascript 使用:

var foo = external.CreateFoo();
alert(foo.Bar);
foo.Bar = "qwer";
alert(external.Bar(foo));

我们不需要为 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:

[ComVisible(true)]
public interface IFoo
{
    string Bar { get; set; }
}

[ComVisible(true)]
public class Foo : IFoo
{
    public string Bar { get; set; }
}

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:

public string Bar(IFoo foo)
{
    return foo.Bar;
}

public IFoo Bar2()
{
    return new Foo() { Bar = "asdf" };
}

Now we can use those types in javascript:

var foo = new ActiveXObject("ClassLibrary1.Foo");
foo.Bar = "qwer";
alert(external.Bar(x));
var fooFromExternal = external.Bar2();
alert(fooFromExternal);
alert(external.Bar(fooFromExternal));

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:

public IFoo CreateFoo()
{
    return new Foo() { Bar = "asdf" };
}

public string Bar(IFoo foo)
{
    return foo.Bar;
}

Javascript usage:

var foo = external.CreateFoo();
alert(foo.Bar);
foo.Bar = "qwer";
alert(external.Bar(foo));

We don't need to register anything for COM interop and there will be no ActiveX warning from IE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文