0
我在控制WebBrowser
时遇到问题。我已将它添加到其中一个窗口,但它没有加载我导航到的页面。我想从其他窗口访问控件,所以我制作了诸如导航等公共方法。我尝试将WebBrowser
添加到其他窗体,并且它似乎正常工作。它在没有任何附加代码的情况下在此窗口上工作。我使用的是AutoResetEvent
,所以当网站加载时会继续执行程序。任何人都可以告诉我这个代码中的问题在哪里?WPF WebBrowser not loading
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
readonly AutoResetEvent thread1Step = new AutoResetEvent(false);
public void EnterForm(string ElementId, string value)
{
HTMLDocument document = (HTMLDocument)TempBrowser.Document;
document.getElementById(ElementId).innerText = value;
}
public void Navigate(string url)
{
TempBrowser.Navigate(url);
thread1Step.WaitOne();
thread1Step.Reset();
}
public void PressButton(string id)
{
HTMLDocument doc = (HTMLDocument)TempBrowser.Document;
IHTMLElement btn = doc.getElementById(id);
if (btn != null)
{
btn.click();
}
}
public void Scroll(int n)
{
HTMLDocument doc = (HTMLDocument)TempBrowser.Document;
doc.parentWindow.scroll(0, n);
}
private void TempBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
thread1Step.Set();
}
public void CallFunction(string Funct)
{
TempBrowser.InvokeScript(Funct);
}
}
您正在使用'thread1Step.WaitOne();'阻止执行导航的线程。所以LoadCompleted永远不能被调用......见例如http://stackoverflow.com/a/20958546/932418(这是一个WinForm示例,但可以很容易地转换为WPF) –
@LB我使用线程的第一个时间,但我设法让你的代码在WPF中工作。但我的主要问题仍然存在。 'WebBrowser'没有加载,所以每次我尝试从它获取文档时,文档都是空的。 –