简体   繁体   English

将本地HTML文件加载到WebBrowser控件中会不断导致循环

[英]loading a local HTML file into the WebBrowser control keeps causing a loop

I am trying to create my own web browser that opens up a web page I created that is stored locally. 我正在尝试创建自己的Web浏览器,以打开我创建的本地存储的网页。 I am new to writing in C# and have gotten the browser to work for the most part but I can not get the web page to open. 我刚开始使用C#编写程序,并且大部分时间都可以使用浏览器,但是无法打开网页。 I have tried several different commands and keep getting the same result.This is the command I am using to open the file: 我尝试了几种不同的命令并保持相同的结果。这是我用来打开文件的命令:

private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
    string curDir = Directory.GetCurrentDirectory();
    var url = new Uri(String.Format("file:///{0}/{1}", curDir, "START_HERE.html"));
    webBrowser1.Navigate(url);
}

The browser opens with no problem but the page keeps loading and doesn't stop. 浏览器可以正常打开,但页面会继续加载并且不会停止。 I tried moving the code to the webBrowser1_Navigating instead and it opens the web browser but the page comes up blank. 我尝试将代码移到webBrowser1_Navigating上,它打开了Web浏览器,但页面空白。 The file is set to copy to Output Directory as Content. 该文件被设置为作为内容复制到输出目录。

I thought it might be the progress bar and tried several different ways of creating it but keep getting the same results. 我认为这可能是进度条,并尝试了几种不同的创建方式,但仍得到相同的结果。

This is the code for the Progress Bar: 这是进度条的代码:

private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
{
    try
    {
        if (e.MaximumProgress != 0)
            ProgressBar1.Value = (int)(((double)e.CurrentProgress * 100) / e.MaximumProgress);
        if (ProgressBar1.Value < 0)
            ProgressBar1.Value = 0;
        else if (ProgressBar1.Value > 100)
            ProgressBar1.Value = 100;    
    }
    catch (Exception ex)
    {           
    }
}

What can I do to fix the loop? 我该怎么做才能修复环路? I know I'm missing something but not sure what. 我知道我想念一些东西,但不确定。

Where your post says 你的帖子说的

private void webBrowser1_Navigated(...)

do you mean "Navigate" instead of "Navigated"? 您是说“导航”而不是“导航”? Because if so, you are causing the loop oby calling Navigate inside of Navigate. 因为如果是这样,您将导致在Navigate内部调用Navigate的循环服从。

You have to go to the properties for the webBroswer control and remove the event handler that is pointed at webBroswer1.Navigated. 您必须转到webBroswer控件的属性,并删除指向webBroswer1.Navigated的事件处理程序。 You probably don't want to have a navigate command in any event handler associated with navigation as you'll probably have unpredictable looping as you're experiencing. 您可能不想在任何与导航相关的事件处理程序中使用导航命令,因为在体验过程中可能会出现不可预测的循环。

Put your code in the webBrowser1.ControlAdded. 将您的代码放在webBrowser1.ControlAdded中。 This gets called when the form gets built and the web browser is added to it's parent container. 在构建表单并将Web浏览器添加到其父容器时调用此方法。 It will only get called once and is independent of the navigation process. 它只会被调用一次,并且与导航过程无关。

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

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