Web 浏览器后退功能不起作用

本文关键字:功能 不起作用 浏览器 Web | 更新日期: 2023-09-27 18:36:42

我已经为我的网络浏览器编写了一个向前和向后导航的方法。目的是将可见的网站(url)存储到一个名为webHistory的列表中。然后,我尝试循环遍历此字符串以进行后退和前进导航。但是,它似乎不起作用。我已经检查并确认该列表正在填充中。这是我的代码。

我的网页浏览器类

  public partial class WebBrowser : Form
{
    public string url;
    public string addressText;
    private homeForm homeForm;
    private List <string> urlList = new List <string> ();
    List<String> webHistory;
    int webHistory_Index;
    bool checkHistory;

    public WebBrowser()
    {
        InitializeComponent();
        webHistory = new List<String>();
        webHistory_Index = 0;
        checkHistory = false;
    }

后退按钮我目前正在测试的那个

 private void backButton_Click(object sender, EventArgs e)
    {
         String backPage = webHistory.ElementAt(webHistory.Count-1);
         webNavigate(backPage);
    }

按钮导航方法

 private void updateNavigation()
    {
        if (webHistory_Index == 0)
        {
            this.backButton.Enabled = false;
        }
        else
        {
            this.backButton.Enabled = true;
        }
        if (webHistory_Index < webHistory.Count)
        {
            this.forwardButton.Enabled = true;
        }
        else
        {
            this.forwardButton.Enabled = false;
        }
    }
    private void navigatedPages(string urlbartext)
    {
        addressText = urlBar.Text;
        urlbartext = "http://" + addressText;
        webHistory.Add(urlbartext);
        if (!checkHistory)
    {
        if (webHistory_Index < webHistory.Count)
        {
            webHistory.RemoveRange(webHistory_Index, webHistory.Count - webHistory_Index);
        }
        System.Diagnostics.Debug.Print(urlbartext + "   -    " + urlBar.SelectedText);
        webHistory_Index += 1;
        updateNavigation();
    }
    checkHistory = false;
    System.Console.WriteLine(webHistory.Count.ToString());
}

Web 浏览器导航方法。

 private void webNavigate(string urlbartext )
    {
        addressText = urlBar.Text;
        urlbartext = "http://" + addressText;
        urlList.Add(urlbartext);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlbartext);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream pageStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(pageStream, Encoding.Default);
        string s = reader.ReadToEnd();
        webDisplay.Text = s;
        reader.Dispose();
        pageStream.Dispose();
        response.Close();
    }

当我单击后退按钮时,当前页面仍然显示并且没有给出任何错误。我哪里出错了?

测试编辑

 List<String> webHistory;
    int curIndex = -1;
    public Form1()
    {
        InitializeComponent();
        webHistory = new List<string>();
    }
    private void gotoUrl(string curUrl)
    {
        curUrl = "http://" + curUrl;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(curUrl);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream pageStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(pageStream, Encoding.Default);
        string s = reader.ReadToEnd();
        webDisplay.Text = s;
        reader.Dispose();
        pageStream.Dispose();
        response.Close();
    }

    private void addUrl(string curUrl)
    {
        if (webHistory.Count > 0 && webHistory.Count - 1 > curIndex) webHistory.RemoveRange(curIndex, webHistory.Count - curIndex - 1);
        webHistory.Add(curUrl);
        curIndex = webHistory.Count - 1;
        gotoUrl(curUrl);
    }
    private void back_Click(object sender, EventArgs e)
    {
        if (curIndex - 1 >= 0)
        {
            curIndex = curIndex - 1;
            gotoUrl(webHistory[curIndex]);
        }
    }
    private void forward_Click(object sender, EventArgs e)
    {
        if (curIndex + 1 <= webHistory.Count - 1)
        {
            curIndex = curIndex + 1;
            gotoUrl(webHistory[curIndex]);
        }
    }
    private void navigate_Click(object sender, EventArgs e)
    {
        addUrl(urlText.Text);
    }

Web 浏览器后退功能不起作用

你的代码中有很多问题。但我认为主要问题是代码的主要结构缺乏清晰度。在这里,您有一个执行所需主要操作的小代码,我希望这将帮助您在不同的前提下重做代码:

List<String> webHistory;
int curIndex = -1;
public Form1()
{
    InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
    webHistory = new List<string>();
}
private void gotoUrl(string curUrl)
{
    //display the url in the browser
}

private void addUrl(string curUrl)
{
    //Add a new Url
    if (webHistory.Count > 0 && webHistory.Count - 1 > curIndex) webHistory.RemoveRange(curIndex, webHistory.Count - curIndex - 1);
    webHistory.Add(curUrl);
    curIndex = webHistory.Count - 1;
    gotoUrl(curUrl);
}

private void Previous_Click(object sender, EventArgs e)
{
    if (curIndex - 1 >= 0)
    {
        //Previous URL
        curIndex = curIndex - 1;
        gotoUrl(webHistory[curIndex]);
    }
}
private void Next_Click(object sender, EventArgs e)
{
    if (curIndex + 1 <= webHistory.Count - 1)
    {
        //Next URL
        curIndex = curIndex + 1;
        gotoUrl(webHistory[curIndex]);
    }
}
private void Navigate_Click(object sender, EventArgs e)
{
    //Simulate the user input by introducing new URLs
    addUrl("");
}

只需在新表单上放置三个按钮:NavigatePreviousNext(并关联相应的Click Events)。此代码以更清晰(和准确)的方式提供您想要的行为。我没有测试太多,但原则上在任何情况下都应该可以正常工作。无论如何,我的目的是帮助您看到方法的问题,以便您可以从头开始重做,而不是提供工作代码供您盲目使用它。