[英]Error when calling Javascript function located in WPF WebBrowser Control from C# code
Based on this solution i tried to call a JavaScript function located in my WebBrowser
- control. 基于此解决方案,我试图调用位于我的
WebBrowser
控件中的JavaScript函数。 The .xaml
looks like this .xaml
看起来像这样
<Grid>
<WebBrowser x:Name="browser"/>
</Grid>
But neither this code 但这个代码都没有
public MainWindow()
{
InitializeComponent();
browser.NavigateToString("<html><script>function callMe() {alert('Hello');} document.myfunc = callMe;</script><body>Hello World</body></html>");
dynamic doc = browser.Document;
doc.myfunc();
}
Error 错误
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: ''mshtml.HTMLDocumentClass' does not contain a definition for 'myfunc''
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:''mshtml.HTMLDocumentClass'不包含'myfunc''的定义
nor this Code 也不是本规范
public MainWindow()
{
InitializeComponent();
browser.NavigateToString("<html><script>function callMe() {alert('Hallo');}</script><body>Hello World</body></html>");
browser.InvokeScript("callMe");
}
Error 错误
System.Runtime.InteropServices.COMException: 'Unknown name.
System.Runtime.InteropServices.COMException:'未知名称。 (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))'
(HRESULT异常:0x80020006(DISP_E_UNKNOWNNAME))'
do work. 做工作。
What am I missing? 我错过了什么?
Try this: 尝试这个:
browser.NavigateToString("<html><script>function callMe() {alert('Hello');} document.myfunc = callMe;</script><body>Hello World</body></html>");
browser.LoadCompleted += (s,e) => browser.InvokeScript("callMe");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.