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