简体   繁体   English

C#WebBrowser对象未加载Uri

[英]C# WebBrowser object not loading Uri

I believe that I've researched here and elsewhere enough to justify posting this as a new question. 我相信我已经在这里和其他地方进行了足够的研究,足以证明将此问题发布为新问题。 My situation is similar but not identical to others' reported problems. 我的情况与其他人报告的问题相似,但并不相同。

As a proof of concept I am building an app that hits the Google StreetView and StaticMaps API's and displays images of them in a C# Windows Form (.net 3.5 for the time being). 作为概念验证,我正在构建一个应用程序,该应用程序可以访问Google StreetView和StaticMaps API,并以C#Windows窗体(暂时为.net 3.5)显示它们的图像。 The form has the WebBrowser object, four TextBoxes (Street, City, State, Zip) for address input and a "Go" button. 该表单具有WebBrowser对象,四个用于输入地址的TextBox(街道,城市,州,邮政编码)和一个“转到”按钮。

Here's my code (and from other reputable posts/sites, this should be all I need): 这是我的代码(以及其他信誉良好的帖子/站点,这应该是我所需要的):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GoogleMapsTest
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      webStreetView = new WebBrowser();
      Uri urlString = new Uri("https://maps.googleapis.com/maps/api/streetview?size=300x300&location=" +
         txtStreet.Text.Replace(" ", "+") + "+" +
         txtCity.Text.Replace(" ", "+") + "+" +
         txtState.Text.Replace(" ", "+") + "+" +
         txtZip.Text.Replace(" ", "+"));
      webStreetView.Navigate(urlString);
    }
  }
}

Unfortunately the form looks like it's going through the motions when the "GO" button is clicked (I can see the hourglass for an instant) but the WebBrowser object remains blank. 不幸的是,当单击“ GO”按钮时,该表单看起来好像正在经历动作(我可以立即看到沙漏),但是WebBrowser对象保持空白。

Here is what I have done to try to test: 这是我所做的尝试测试:

  1. Plugged a test URL into the URL property of the object in design mode - form loads with the image 在设计模式下将测试URL插入对象的URL属性中-随图像一起加载表单
  2. Pulled the URI address out of the app in debug and tested it in my browser - works fine 在调试中从应用程序中拉出URI地址,并在我的浏览器中对其进行了测试-正常工作
  3. Per advice found for similar problems, I tried adding in a call to WebBrowser.Refresh() - no dice 根据发现类似问题的建议,我尝试添加对WebBrowser.Refresh()的调用-没有骰子
  4. Please note that the line "webStreetView = new WebBrowser();" 请注意,“ webStreetView = new WebBrowser();”行 was put in as a troubleshooting attempt per my other comment. 根据我的其他评论,它被用作故障排除尝试。

It's probably something really simple that I'm overlooking. 我可能忽略了一些非常简单的事情。 If anyone can help point me in the right direction and find what I'm missing it would be greatly appreciated. 如果有人能帮助我指出正确的方向并找到我所缺少的内容,将不胜感激。

OK, I figured it out. 好,我知道了。 Apparently the default margins (7,7,7,7) for the WebBrowser object are the bare minimum (I had set them to zero in an attempt to just show a clean image with no border). 显然,WebBrowser对象的默认边距(7,7,7,7)是最低要求(我已将它们设置为零,以尝试显示无边框的干净图像)。

For those who attempted to answer this question for me in earnest, I thank you kindly. 对于那些想为我认真回答这个问题的人,我表示感谢。

For the person who downvoted my question: go fish. 对于拒绝我的问题的人:去钓鱼。

Do not create a new instance of the web browser. 不要创建Web浏览器的新实例。 Remove webStreetView = new WebBrowser(); 删除webStreetView = new WebBrowser(); and it should work. 它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM