Csharp/C#教程:WebBrowser导航function不起作用,并且不调用处理程序分享


WebBrowser导航function不起作用,并且不调用处理程序

代码如下。

我试图导航到一个网站并阅读信息,问题是Navigate不起作用,唯一被调用的事件是导航,打印的Url为空,其他事件从未被调用。 我错过了什么? 我必须使用Form类才能导航? 我不能从控制台应用程序以编程方式使用它吗?

请帮忙。

class WebNavigator { private readonly WebBrowser webBrowser; public WebNavigator() { webBrowser = new WebBrowser { AllowNavigation = true }; webBrowser.Navigated += webBrowser_Navigated; webBrowser.Navigating += webBrowser_Navigating; webBrowser.DocumentCompleted += webBrowser_DocumentCompleted; } // Navigates to the given URL if it is valid. public void Navigate(string address) { if (String.IsNullOrEmpty(address)) return; if (address.Equals("about:blank")) return; if (!address.StartsWith("https://") && !address.StartsWith("https://")) { address = "https://" + address; } try { Trace.TraceInformation("Navigate to {0}", address); webBrowser.Navigate(new Uri(address)); } catch (System.UriFormatException) { Trace.TraceError("Error"); return; } } // Occurs when the WebBrowser control has navigated to a new document and has begun loading it. private void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e) { Trace.TraceInformation("Navigated to {0}", webBrowser.Url); } // Occurs before the WebBrowser control navigates to a new document. private void webBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e) { Trace.TraceInformation("Navigating to {0}", webBrowser.Url); } private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { var wb = sender as WebBrowser; Trace.TraceInformation("DocumentCompleted {0}", wb.Url); } } 

我会使用await / async 。

用法:

 static async void DoWork() { var html = await GetHtmlAsync("https://www.google.com/"); } 

实用方法

 static Task GetHtmlAsync(string url) { var tcs = new TaskCompletionSource(); var thread = new Thread(() => { WebBrowser wb = new WebBrowser(); WebBrowserDocumentCompletedEventHandler documentCompleted = null; documentCompleted = async (o, s) => { wb.DocumentCompleted -= documentCompleted; await Task.Delay(2000); //Run JS a few seconds more tcs.TrySetResult(wb.DocumentText); wb.Dispose(); Application.ExitThread(); }; wb.ScriptErrorsSuppressed = true; wb.DocumentCompleted += documentCompleted; wb.Navigate(url); Application.Run(); }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); return tcs.Task; } 

你提到这是一个控制台应用程序。 要使WebBrowser在控制台应用程序中工作,您需要确保浏览器的线程是一个STA线程( thread.SetApartmentState(ApartmentState.STA )。另外,为了能够处理事件,线程必须泵送消息( Application.Run )考虑到这一点,您可能希望创建一个单独的线程(从主控制台线程)来运行WebBrowser。

谢谢你的回答。 通过添加包含Application.DoEvents()的while子句解决了这个问题。

  webBrowser.Navigate(new Uri(address)); while (!navigationCompleted) { Application.DoEvents(); Thread.Sleep(navigationPollInterval); } 

我不确定这是否是最佳解决方案,我将很高兴听到更好的解决方案。

上述就是C#学习教程:WebBrowser导航function不起作用,并且不调用处理程序分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/987907.html

(0)
上一篇 2021年12月23日
下一篇 2021年12月23日

精彩推荐