Web浏览器不会触发LoadComplete,但导航射击

时间:2016-10-07 13:06:47

标签: c# wpf user-controls webbrowser-control

我有userControl(wpf)

    public WebBrowserControl()
    {
       InitializeComponent();
        _Browser = new WebBrowser();
        _pipeClient = new NamedPipeClient<WebMessage>("TestPipe");
        _pipeClient.ServerMessage += PipeClientOnServerMessage;
        _pipeClient.Error += PipeClientOnError;
        _pipeClient.Start();
        InternetExplorerBrowserEmulation.SetBrowserEmulationMode();
        SuppressScriptErrors(_Browser, false);
        SetWebBrowserFeatures();
        GridBrrw.Children.Add(_Browser);
       _Browser.ObjectForScripting = new ObjectForScripting(_pipeClient);
        _Browser.LoadCompleted += new LoadCompletedEventHandler(_Browser_OnLoadCompleted);
        _Browser.Navigating += _Browser_OnNavigating;

        var th = new Thread(ExecuteInForeground);
        th.Start();

    }

    private void ExecuteInForeground()
    {
        int i = 0;
        while (i<=9)
        {
            Thread.Sleep(1000);
            _pipeClient.PushMessage(new WebMessage() {Actions = "allo"});
            i++;
        }
    }
    private void _Browser_OnNavigating(object sender, NavigatingCancelEventArgs e)
    {
        if (IsClick)
        {
            var mes = new WebMessage { Actions = "OpenUrl" };
            mes.Url = e.Uri.AbsoluteUri;
            _pipeClient.PushMessage(mes);
            e.Cancel = false;
        }
        return;
        e.Cancel = false;

    }
     private void _Browser_OnLoadCompleted(object sender, NavigationEventArgs e)
    {
        try
        {

            var br = sender as WebBrowser;
            if (br?.Source != null && br.Source.AbsoluteUri != e.Uri.AbsoluteUri)
            {
                MessageBox.Show($"Source = {br.Source.AbsoluteUri},\r\n AbsoluteUri = {e.Uri.AbsoluteUri}");
                return;
            }
            Document = (HTMLDocument)br.Document;
            if (!string.IsNullOrEmpty(FindElement))
            {
               var node =  HtmlNode.CreateNode(FindElement);
                while (GetElement(node) == null)
                {
                    System.Windows.Forms.Application.DoEvents();
                }
            }
            if (WaitAjax)
            {
                ConnectToAjax();
                return;
            }
            if (Sleep > 0)
            {
                var time = TimeSpan.FromSeconds(Sleep);
                Thread.Sleep(time);
            } 
            var mes = new WebMessage { Actions = "Load" };
            mes.Title = Document.title;

            mes.Url = br.Source.AbsoluteUri;
            mes.Domain = br.Source.Host.Replace("http", "").Replace("http://", "").Replace("https://", "").Replace("https", "");
            mes.Fovicon = $"http://www.google.com/s2/favicons?domain={mes.Domain}";
            if (Document != null)
            {
                var htmls = Document.getElementsByTagName("html");
                if (htmls != null && htmls.length > 0)
                {
                    var html = htmls.item(0) as IHTMLElement;
                    mes.Html = html.outerHTML;
                }
            }

            _pipeClient.PushMessage(mes);
        }
        catch (Exception ex)
        {

            throw ex;
        }


    }

事件导航触发,ExecuteInForeground发送消息,但LoadCompleted事件未触发。这可能是由于设置窗口 - Property =“ResizeMode”Value =“NoResize”。我做错了什么?

0 个答案:

没有答案