2

我有一个包含 Frame 控件的 WPF 应用程序。框架控件显示 html 内容。html 页面包含一个 JavaScript 函数,我想在 Frame 和 WPF 代码加载 html 页面后调用该函数?有没有一种方法可以调用在 WPF 框架控件和 WPF 代码中加载的 html 页面的 JavaScript 方法?

4

3 回答 3

3

您不能与框架内的 HTML 进行交互。为了与 HTML 交互,您可以使用 .NET 3.5 SP1 的 WPF 的 WebBrowser 控件。

您可以在此视频中看到正在运行的 WebBrowser 控件:

http://channel9.msdn.com/posts/AdamKinney/WPF-35-SP1-App-Model-with-Jennifer-Lee/

于 2009-03-06T08:29:46.860 回答
1

无法访问 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>
于 2009-03-06T08:14:29.440 回答
0

Use <body onload="javascript:alert('hi its loaded. do what you want here');"> in your HTML page, say myhtmlpage.html.

Then use a frame XAML control (<Frame Name="myWpfFrame" Source="http://myurl/myhtmlpage.html">) in your WPF code.


This solution will work assuming you want to call the javascript only once and thats only after the HTML page is loaded.

If you want to call the js more than once, you can possibly set the source property of the frame control from code as and when you need... as in .. myWpfFrame.Source = myWpfFrame.Source;

于 2011-07-08T13:32:30.697 回答