我有一个包含框架控件的WPF应用程序。frame控件显示html内容。html页面包含一个JavaScript函数,我想在html页面加载到框架上并从WPF代码中调用该函数。有没有方法可以调用加载在WPF框架控件和WPF代码中的html页面的JavaScript方法?
发布于 2009-03-06 08:29:47
您不能与框架内的HTML交互。为了与超文本标记语言交互,您可以使用.NET 3.5 SP1的WebBrowser控件。
您可以在此视频中看到WebBrowser控件的实际效果:
http://channel9.msdn.com/posts/AdamKinney/WPF-35-SP1-App-Model-with-Jennifer-Lee/
发布于 2009-03-06 08:14:29
不能访问DOM,所以恐怕不能使用。如果你确实需要它,那么你最好的选择就是嵌入来自System.Windows.Forms的WinForms WebBrowser控件。
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:f="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:fi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration.dll" >
<Grid>
<fi:WindowsFormsHost>
<f:WebBrowser x:Name="BrowserControl" Url="http://www.myurl.com/"/>
</fi:WindowsFormsHost>
</Grid>
</Page>发布于 2011-07-08 21:32:31
在你的超文本标记语言页面中使用<body onload="javascript:alert('hi its loaded. do what you want here');">,比如myhtmlpage.html.
然后在WPF代码中使用frame XAML控件(<Frame Name="myWpfFrame" Source="http://myurl/myhtmlpage.html">)。
这个解决方案假设你只想调用javascript 一次,也就是只在加载HTML页面之后调用。
如果您想多次调用js,您可以在需要时从代码中设置frame控件的source属性。就像..myWpfFrame.Source = myWpfFrame.Source;
https://stackoverflow.com/questions/617939
复制相似问题