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'沒有加載,所以每次我嘗試從它獲取文檔時,文檔都是空的。 –