Call TypeScript function from C# webbrowser control

£可爱£侵袭症+ 提交于 2019-12-02 07:54:20

问题


I have WinForm with WebBrowser control where I open HTML5 + Angular JS (TypeScript) form. I want to call typescript function from my C# code but it is not working with InvokeScirpt() method.

webBrowser1.Document.InvokeScript("method", new object[] {"123", "453")});

I am able to call Javascript method using this way when i load HTML + Javascript page in webbrowser control.

Also, please note typescript function I am able to call from the HTML page I have (on button click)

Could you please suggest whether this is a right way to call typescript function from C# webbrowser control OR I need to try something else?

Thanks,


回答1:


Typescript methods will compile to javascript methods when you build your application, so to call them, you can call compiled methods like any other javascript methods.

Example

In the example, I suppose you have a app.ts containing this class:

class Greeter {
    greeting: string;
    constructor(message: string) {
        this.greeting = message;
    }
    greet() {
        return "Hello, " + this.greeting;
    }
}

And I suppose you have added app.js in index.html add this code:

<html>
<head>
    <script src="app.js"></script>
</head>
<body>
    ...
</body>
</html>

Then in your windows forms application, you can use Greeter this way:

string javascript = "var greeter = new Greeter('World!'); alert(greeter .greet());";
webBrowser1.Document.InvokeScript("eval", new object[] { javascript });


来源:https://stackoverflow.com/questions/41264672/call-typescript-function-from-c-sharp-webbrowser-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!