WebBrowser.Navigate just… Isn't

笑着哭i 提交于 2020-01-17 07:39:21

问题


I have this code:

private void goButton_Click(object sender, EventArgs e)
{
    web.Navigate(loginURL.Text + "/auth/login");
}

I have the browser showing, and it's just not navigating... It doesn't navigate etc.

The URL is valid.


回答1:


MSDN is your friend. Make sure you have the 'http://' prefix and try using the Navigate(Uri url) overload.

// Navigates to the given URL if it is valid. 
private void Navigate(String address)
{
    if (String.IsNullOrEmpty(address)) 
         return;

    if (address.Equals("about:blank")) 
         return;

    if (!address.StartsWith("http://") && !address.StartsWith("https://"))
    {
        address = "http://" + address;
    }
    try
    {
        webBrowser.Navigate(new Uri(address));
    }
    catch (System.UriFormatException)
    {
        return;
    }
}



回答2:


You need to handle DocumentCompleted event of web browser.

Go through following code:

  private void goButton_Click(object sender, EventArgs e)
    {
      WebBrowser wb = new WebBrowser();
      wb.AllowNavigation = true;

      wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);

      wb.Navigate(loginURL.Text + "/auth/login");

              }

    private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
      WebBrowser wb = sender as WebBrowser;
      // wb.Document is not null at this point
    }


来源:https://stackoverflow.com/questions/18050778/webbrowser-navigate-just-isnt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!