现在的位置: 首页 > 综合 > 正文

C#使用双链表来简单模拟IE前进后退功能

2012年02月16日 ⁄ 综合 ⁄ 共 4343字 ⁄ 字号 评论关闭

简单的测试了一下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的方便之处了.对性能的处理请自已把握.

好了,如果有不合理的地方请大家指正.

引用地址:  http://www.cnblogs.com/hcl0208/archive/2009/04/18/1438641.html

抱歉!评论已关闭.