C# WPF 帧控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/617939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Frame control
提问by
I have a WPF application containing a Frame control. The frame control displays the html content. The html page contains a JavaScript function which I want to call after html page is loaded on Frame and from the WPF code ? Is there a way I can call JavaScript method of html page loaded in WPF frame control and from within WPF code?
我有一个包含 Frame 控件的 WPF 应用程序。框架控件显示 html 内容。html 页面包含一个 JavaScript 函数,我想在 html 页面加载到 Frame 和 WPF 代码后调用它?有没有办法可以调用在 WPF 框架控件和 WPF 代码中加载的 html 页面的 JavaScript 方法?
回答by Steven Robbins
There's no access to the DOM, so it's a no go I'm afraid. If it's something you definately need then your best bet is probably to embed the WinForms WebBrowser control from System.Windows.Forms.
无法访问 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>
回答by Eriawan Kusumawardhono
You can't interact with the HTML inside of a frame. In order to interact with HTML, you can use WPF's WebBrowser control of .NET 3.5 SP1.
您无法与框架内的 HTML 进行交互。为了与 HTML 交互,您可以使用 WPF 的 .NET 3.5 SP1 的 WebBrowser 控件。
You can see in WebBrowser control in action in this video:
您可以在此视频中看到正在运行的 WebBrowser 控件:
http://channel9.msdn.com/posts/AdamKinney/WPF-35-SP1-App-Model-with-Jennifer-Lee/
http://channel9.msdn.com/posts/AdamKinney/WPF-35-SP1-App-Model-with-Jennifer-Lee/
回答by cethie
Use <body onload="javascript:alert('hi its loaded. do what you want here');">in your HTML page, say myhtmlpage.html.
使用<body onload="javascript:alert('hi its loaded. do what you want here');">在HTML页面中,说myhtmlpage.html。
Then use a frame XAML control (<Frame Name="myWpfFrame" Source="http://myurl/myhtmlpage.html">) in your WPF code.
然后<Frame Name="myWpfFrame" Source="http://myurl/myhtmlpage.html">在 WPF 代码中使用框架 XAML 控件 ( )。
This solution will work assuming you want to call the javascript only onceand thats only afterthe HTML page is loaded.
假设您只想调用 javascript一次,并且仅在加载 HTML 页面之后,此解决方案将起作用。
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;
如果您想多次调用 js,您可以在需要时从代码中设置框架控件的源属性......如.. myWpfFrame.Source = myWpfFrame.Source;

