问题
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