您现在的位置是:首页 >

生活只能前进不能后退 C#使用双链表来实现模拟IE前进后退功能

火烧 2023-01-29 11:24:08 1048
C#使用双链表来实现模拟IE前进后退功能   简单的测试了一下IE前进和后退的过程  依次访问网站A B C D   后退至 B   然后重新请求网站E  则记录的保存顺序则是 A B E  C D将

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

  简单的测试了一下IE前进和后退的过程

  依次访问网站A B C D

  后退至 B

  然后重新请求网站E

  则记录的保存顺序则是 A B E

  C D将会从记录列表中删除

  下面看代码(以下操作均在内存中进行):

  一个History对象 用来生成一个记录对象 该对象包含 url title 三个属性

  Codeclass 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是对链表操作静态类 具体看代码注释

  Codeclass 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)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 > ){if (HistoryList Count + > 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);}}

  页面调用方法

  Codeprivate 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(); //刷新按钮状态 由自已定义}

生活只能前进不能后退 C#使用双链表来实现模拟IE前进后退功能

  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的方便之处了 对性能的处理请自已把握

lishixinzhi/Article/program/net/201311/12193  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码