Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从WPF MainWindowLoad()调用WinForm WebBrowser;DocumentLoadComplted在MainWindowLoad之后激发_C#_Wpf_Winforms_Browser_Webbrowser Control - Fatal编程技术网

C# 从WPF MainWindowLoad()调用WinForm WebBrowser;DocumentLoadComplted在MainWindowLoad之后激发

C# 从WPF MainWindowLoad()调用WinForm WebBrowser;DocumentLoadComplted在MainWindowLoad之后激发,c#,wpf,winforms,browser,webbrowser-control,C#,Wpf,Winforms,Browser,Webbrowser Control,我需要从网页上获取一些信息,并将其显示在主窗口上。我正在调用一个在单独的库中编写的例程,该库的属性将这些信息提供给加载网页后的用户 我在stackoverflow上做了很多研究,发现了各种各样的解决方案;但没有一个对我有效。以下是我的常规: protected System.Windows.Forms.WebBrowser wb; public void ObtainProfileInformation() { url = CUSTOMER

我需要从网页上获取一些信息,并将其显示在主窗口上。我正在调用一个在单独的库中编写的例程,该库的属性将这些信息提供给加载网页后的用户

我在stackoverflow上做了很多研究,发现了各种各样的解决方案;但没有一个对我有效。以下是我的常规:

protected System.Windows.Forms.WebBrowser wb;
        public void ObtainProfileInformation()
        {
            url = CUSTOMER_PROFILE_URL;
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri(url));
            try
            {
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                var th = new System.Threading.Thread(() =>
                {
                    wb = new System.Windows.Forms.WebBrowser() { Visible = false, ScriptErrorsSuppressed = true };
                    wb.DocumentCompleted += (sender, e) =>
                    {
                        string target_url = wb.Url.AbsoluteUri;
                        if ((target_url.Contains("login") == false))
                        {
                            var html_text = wb.DocumentText;
                            HtmlAgilityPack.HtmlDocument html_document = new HtmlAgilityPack.HtmlDocument();
                            html_document.LoadHtml(html_text);
                            foreach (var MatchingDiv in html_document.DocumentNode.Descendants("span"))
                            {
                                if (MatchingDiv.Id.Contains("lblFullName"))
                                {
                                    CustomerName = MatchingDiv.InnerText;
                                }
                                if (MatchingDiv.Id.Contains("lblCompany"))
                                {
                                    CompanyName = MatchingDiv.InnerText;
                                }
                                if (MatchingDiv.Id.Contains("lblTitle"))
                                {
                                    CustomerTitle = MatchingDiv.InnerText;
                                }
                                if (MatchingDiv.Id.Contains("lblEmail"))
                                {
                                    CustomerEmail = MatchingDiv.InnerText.Split(new char[] { ':' })[1].TrimStart();
                                }
                                if (MatchingDiv.Id.Contains("lblUsername"))
                                {
                                    CustomerUserName = MatchingDiv.InnerText.Split(new char[] { ':' })[1].TrimStart();
                                }
                            }
                        }
                        System.Windows.Forms.Application.ExitThread();
                    };
                    wb.Navigate(url);
                    System.Windows.Forms.Application.Run();
                });
                th.SetApartmentState(System.Threading.ApartmentState.STA);
                th.Start();
            }
            catch (WebException)
            {
                wb.Dispose();
            }
            catch (Exception ex)
            {
                wb.Dispose();
                throw new Exception("Something strange", ex);
            }
        }

DocumentCompleted事件被触发,CustomerName等属性被填充,但在窗口的加载事件完成后。因此我无法在窗口上更改它们。

您需要在
窗口上执行此操作。onload
事件,示例:看起来很熟悉,我的版本可读。您必须调用th.Join()以等待结果可用。@HansPassant:谢谢,我们能够获得我想要的结果