win8页面导航--删除当前页面堆栈信息

win8中导航很容易,仅一行代码就可实现,但是如果我不想让某些页面保存在导航堆栈中怎么办?微软目前没有提供这样的功能。

  要想实现这样的功能需要从堆栈历史记录下手,从farm.GetNavigationState()这个函数找到突破口。

  来看看farm.GetNavigationState()返回的信息格式:

  1,3,1,37,Page1,0,39,Page2,0,34,Page3,0

   第一个数字1和最后一个数字0是 固定格式,如果堆栈信息是空的,那么堆栈的记录就是“1,0”;

   第二个数字3代表当前堆栈中页面的个数;

   第三个数字1则是当前显示的页面在堆栈中的索引;

   第四个数字37暂时理解成页面的ID吧;

   第五个则是页面名称了;

  所以堆栈的格式如下

  1,页面数量,当前页面索引,页面1ID,页面1名称,0,页面2ID,页面2名称,...,0

  了解堆栈格式就好办了,开始按照我们的需求写代码吧。

   首先我想把当前页面信息从堆栈中删除,代码如下:

  public static void RemoveCurrent(this Frame farm)
        {
            if (farm == null)
                farm = Window.Current.Content as Frame;

            if (farm == null) return;

            string navigationHistory = farm.GetNavigationState();

            List<string> history = new List<string>(navigationHistory.Split(','));

            if (history.Count <= 2) return;

            history.RemoveRange(history.Count - 2, 2);

            int numberOfPages = int.Parse(history[1]);
            int currentPageIndex = int.Parse(history[2]);

            numberOfPages--;
            currentPageIndex--;

            history[1] = numberOfPages.ToString();
            history[2] = currentPageIndex.ToString();

            string newHistory = String.Join<string>(",", history.AsEnumerable());

            farm.SetNavigationState(newHistory);                

        }

   使用的时候像这样this.Frame.RemoveCurrent();就可以了

    那么清空我要清空整个堆栈信息呢?看下面代码:

   

public static void ClearAll(this Frame farm)
        {
            if (farm == null)
                farm = Window.Current.Content as Frame;

            if (farm != null)
                farm.SetNavigationState("1,0");
        }
    调用方法同上this.Frame.ClearAll();

    整个导航工具的代码:

 public static class NavigateStackUtil
    {
        public static void ClearAll(this Frame farm)
        {
            if (farm == null)
                farm = Window.Current.Content as Frame;

            if (farm != null)
                farm.SetNavigationState("1,0");
        }

        public static void RemoveCurrent(this Frame farm)
        {
            if (farm == null)
                farm = Window.Current.Content as Frame;

            if (farm == null) return;

            string navigationHistory = farm.GetNavigationState();

            List<string> history = new List<string>(navigationHistory.Split(','));

            if (history.Count <= 2) return;

            history.RemoveRange(history.Count - 2, 2);

            int numberOfPages = int.Parse(history[1]);
            int currentPageIndex = int.Parse(history[2]);

            numberOfPages--;
            currentPageIndex--;

            history[1] = numberOfPages.ToString();
            history[2] = currentPageIndex.ToString();

            string newHistory = String.Join<string>(",", history.AsEnumerable());

            farm.SetNavigationState(newHistory);                

        }

        public static void GoBack(this Frame farm)
        {
            if (farm == null)
                farm = Window.Current.Content as Frame;

            if (farm != null && farm.CanGoBack) farm.GoBack();
        }

        public static void GoForward(this Frame farm)
        {
            if (farm == null)
                farm = Window.Current.Content as Frame;

            if (farm != null && farm.CanGoBack) farm.GoForward();
        }

        public static void GoHome(this Frame farm)
        {
            if (farm == null)
                farm = Window.Current.Content as Frame;

            if (farm == null) return;

            string navigationHistory = farm.GetNavigationState();

            List<string> history = new List<string>(navigationHistory.Split(','));

            if (history.Count <= 6) return;

            history.RemoveRange(6, history.Count-6);

            history[1] = "1";
            history[2] = "0";

            string newHistory = String.Join<string>(",", history.AsEnumerable());

            farm.SetNavigationState(newHistory);                

        }
    }

  欢迎大家多多交流。

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值