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