【问题标题】:Load a local webpage into the webbrowser control将本地网页加载到 webbrowser 控件中
【发布时间】:2014-04-09 21:32:08
【问题描述】:

我正在尝试简单地将 webbrowser 控件添加到窗口,然后让它打开一个页面。我尝试了一个 Web URL 以及一个本地 HTML 文件,但无济于事。这是我的代码:

命名空间 qTab1 { 公共部分类Form1:表格 { 公共表格1() { 初始化组件(); }

    private void Form1_Load(object sender, EventArgs e)
    {
        FileStream source = new FileStream("index.html", FileMode.Open, FileAccess.Read);
        webBrowser1.DocumentStream = source;
        //// When the form loads, open this web page.
        //webBrowser1.Navigate("www.google.com");
    }

    private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
    {
        // Set text while the page has not yet loaded.
        this.Text = "Navigating";
    }

    private void webBrowser1_DocumentCompleted(object sender,
        WebBrowserDocumentCompletedEventArgs e)
    {
        // Better use the e parameter to get the url.
        // ... This makes the method more generic and reusable.
        this.Text = e.Url.ToString() + " loaded";
    }
}

}

这是我目前的项目:

我做错了什么?

【问题讨论】:

    标签: c# winforms controls


    【解决方案1】:

    发生这种情况的原因是,当您按 Debug 或以任何其他方式构建项目时,根目录就是可执行文件的目录(所以它会是 - ./bin/Debug ),而不是项目的目录。

    要解决此问题,您可以执行以下操作:

    1. 右键单击 html 文件,单击属性并将变量“复制到输出目录”设置为始终复制。这样,html 文件将与您的可执行文件一起复制。
    2. 现在您必须将本地文件加载到 WebBrowser 控件中。以下应该有效:

      webBrowser1.Url = new Uri("file:///" + Directory.GetCurrentDirectory() + "/index.html");

    【讨论】:

    • 你说的都是有道理的,但是在你的建议之后我仍然得到一个空白页面,没有任何加载。
    • 我以为我做错了什么。刚刚做了一个快速检查(按照我写的相同步骤),一切似乎都很好。请检查您的 ./bin/Debug 文件夹。它是否包含 index.html?
    猜你喜欢
    • 1970-01-01
    • 2012-07-18
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多