简单的测试了一下IE前进和后退的过程.
依次访问网站A,B,C,D.
后退至 B,
然后重新请求网站E,
则记录的保存顺序则是 A,B,E
C,D将会从记录列表中删除.
下面看代码(以下操作均在内存中进行):
一个History对象,用来生成一个记录对象,该对象包含 url,title,html三个属性.
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
class History
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private string Title_ = "";
private string WmlSource_ = "";
private string Url_ = "";
public string Title
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return Title_; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ Title_ = value; }
}
public string WmlSource
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return WmlSource_; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ WmlSource_ = value; }
}
public string Url
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return Url_; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ Url_ = value; }
}
public History()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
public History(string t, string w, string u)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Title_ = t;
WmlSource_ = w;
Url_ = u;
}
}
HistoryAction是对链表操作静态类,具体看代码注释
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
class HistoryAction
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//活动节点对象,即当前的节点对象
private static LinkedListNode<History> HistoryCurrentNode= null;
//全局的链表对象,所以记录均保存到该对象中
private static LinkedList<History> HistoryList = new LinkedList<History>();
//设置保存最大条数,当达到该条数时,每次增加记录时,均依次删除原有记录
private static int MaxList = 10;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 或取当前的记录信息
/// </summary>
public static History CurrentHistory
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return (History)HistoryCurrentNode.Value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 当前后退时否可用,用于设置按钮状态信息
/// </summary>
public static bool IsBack
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return HistoryCurrentNode.Next == null ? false : true;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 当前前进时否可用,用于设置按钮状态信息
/// </summary>
public static bool IsGo
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return HistoryCurrentNode.Previous == null ? false : true;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 向历史记录链表中加入新的节点
/// </summary>
/// <param name="h"></param>
public static void Add(History h)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
LinkedListNode<History> tem = HistoryList.First;
//如果连续加入url相同的记录,则只加入一次,可以根据自已情况设置
if (tem!=null && ((History)tem.Value).Url.ToLower() == h.Url.ToLower())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//当当前节点不为空,或该节点的上一个节点也不为空时,则删除该节点的前所有节点(模拟IE)
//模拟IE对前进后退的处理
if (HistoryCurrentNode != null && HistoryCurrentNode.Previous != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DelNode(HistoryCurrentNode);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//处理限制最大记录条数
if (MaxList > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (HistoryList.Count + 1 > MaxList)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HistoryList.RemoveLast();
}
}
HistoryCurrentNode = new LinkedListNode<History>(h);
HistoryList.AddFirst(HistoryCurrentNode);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 后退
/// </summary>
public static void Back()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HistoryCurrentNode = HistoryCurrentNode.Next;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 前进
/// </summary>
public static void Go()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HistoryCurrentNode = HistoryCurrentNode.Previous;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 删除指定节点前所有节点
/// </summary>
/// <param name="node"></param>
private static void DelNode(LinkedListNode<History> node)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
while (node.Previous != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HistoryList.Remove(node.Previous);
}
}
}
页面调用方法
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
private void AddHistory(string title, string wmlsource, string url) //将记录加到列表中
{
History h = new History();
h.Title = title;
h.WmlSource = wmlsource;
h.Url = url;
HistoryAction.Add(h);
RefurbishGoBackButton(); //刷新按钮状态.由自已定义
}
private void Back() //后退
{
HistoryAction.Back();
History h = HistoryAction.CurrentHistory; //获取后退后的History对象
LoadHistory(h); //处理该对象,由自已定义.
RefurbishGoBackButton();//刷新按钮状态.由自已定义
}
private void Go() //前进
{
HistoryAction.Go();
History h = HistoryAction.CurrentHistory;
LoadHistory(h); //处理该对象,由自已定义.
RefurbishGoBackButton();//刷新按钮状态.由自已定义
}
OK,搞定,实际上非常简单,这里可以看到LinkedList的方便之处了.对性能的处理请自已把握.
好了,如果有不合理的地方请大家指正.