问题描述
我使用WebBrowser控件的WinForm来打开HTML5 + Angular JS(TypeScript)窗体。我想从我的C#代码调用打字稿功能,但它不适用于InvokeScirpt()方法。
webBrowser1.Document.InvokeScript(method,new object [] {123,453)});
我可以使用这种方式调用Javascript方法,当我在webbrowser控件中加载HTML + Javascript页面时。
另外,请注意打字稿功能我是能够从我的HTML页面调用(点击按钮)
您能否建议这是从C#webbrowser控件调用打字稿功能的正确方式还是我需要尝试别的东西?
谢谢,
在构建应用程序时编译为JavaScript方法,因此要调用它们,可以像调用任何其他JavaScript方法一样调用编译方法。
示例
在示例中,我想您有一个<$ c
class Greeter {
包含这个类:串;
greeting:$ c> app.ts
构造函数(消息:字符串){
this.greeting = message;
}
greet(){
returnHello,+ this.greeting;
$ / code>
我想你已经添加了 app.js
在 index.html
添加此代码:
< HTML>
< head>
< script src =app.js>< / script>
< / head>
< body>
...
< / body>
< / html>
然后在windows窗体应用程序中,可以使用 Greeter
string javascript =var greeter = new Greeter('World!'); alert(greeter。迎接());;
webBrowser1.Document.InvokeScript(eval,new object [] {javascript});
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,
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 });
这篇关于从C#webbrowser控件调用TypeScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!