簡單的測試了一下IE前進和後退的過程.
依次訪問網站A,B,C,D.
後退至 B,
然後重新請求網站E,
則記錄的保存順序則是 A,B,E
C,D將會從記錄列表中刪除.
下面看代碼(以下操作均在內存中進行):
一個History對象,用來生成一個記錄對象,該對象包含 url,title,html三個屬性.
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是對鏈表操作靜態類,具體看代碼注釋
class HistoryAction
{
//活動節點對象,即當前的節點對象
private static LinkedListNode<History> HistoryCurrentNode= null;
//全局的鏈表對象,所以記錄均保存到該對象中
private static LinkedList<History> HistoryList = new LinkedList<History>();
//設置保存最大條數,當達到該條數時,每次增加記錄時,均依次刪除原有記錄
private static int MaxList = 10;
/**//// <summary>
/// 或取當前的記錄信息
/// </summary>
public static History CurrentHistory
{
get { return (History)HistoryCurrentNode.Value; }
}
/**//// <summary>
/// 當前後退時否可用,用於設置按鈕狀態信息
/// </summary>
public static bool IsBack
{
get
{
return HistoryCurrentNode.Next == null ? false : true;
}
}
/**//// <summary>
/// 當前前進時否可用,用於設置按鈕狀態信息
/// </summary>
public static bool IsGo
{
get
{
return HistoryCurrentNode.Previous == null ? false : true;
}
}
/**//// <summary>
/// 向歷史記錄鏈表中加入新的節點
/// </summary>
/// <param name="h"></param>
public static void Add(History h)
{
LinkedListNode<History> tem = HistoryList.First;
//如果連續加入url相同的記錄,則只加入一次,可以根據自已情況設置
if (tem!=null && ((History)tem.Value).Url.ToLower() == h.Url.ToLower())
{
return;
}
//當當前節點不為空,或該節點的上一個節點也不為空時,則刪除該節點的前所有節點(模擬IE)
//模擬IE對前進後退的處理
if (HistoryCurrentNode != null && HistoryCurrentNode.Previous != null)
{
DelNode(HistoryCurrentNode);
}
//處理限制最大記錄條數
if (MaxList > 0)
{
if (HistoryList.Count + 1 > MaxList)
{
HistoryList.RemoveLast();
}
}
HistoryCurrentNode = new LinkedListNode<History>(h);
HistoryList.AddFirst(HistoryCurrentNode);
}
/**//// <summary>
/// 後退
/// </summary>
public static void Back()
{
HistoryCurrentNode = HistoryCurrentNode.Next;
}
/**//// <summary>
/// 前進
/// </summary>
public static void Go()
{
HistoryCurrentNode = HistoryCurrentNode.Previous;
}
/**//// <summary>
/// 刪除指定節點前所有節點
/// </summary>
/// <param name="node"></param>
private static void DelNode(LinkedListNode<History> node)
{
while (node.Previous != null)
{
HistoryList.Remove(node.Previous);
}
}
}
頁面調用方法
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的方便之處了.對性能的處理請自已把握.
好了,如果有不合理的地方請大家指正.