0

我为我的网络浏览器编写了一个向前和向后导航的方法。目的是将访问过的站点(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);
    }
4

1 回答 1

1

您的代码中有很多问题。但我认为主要问题是代码的主要结构缺乏明确性。在这里,您有一个小代码执行您想要的主要操作,我希望它可以帮助您在不同的场所重做您的代码:

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)。此代码以更清晰(和准确)的方式提供您想要的行为。我没有对其进行太多测试,但原则上在任何情况下都应该可以正常工作。无论如何,我的目的是帮助你发现你的方法存在的问题,这样你就可以从头开始重做它,而不是提供一个工作代码让你盲目地使用它。

于 2013-10-24T09:59:53.097 回答