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

来源:互联网 发布:美国退出巴黎协定 知乎 编辑:程序博客网 时间:2024/05/16 04:43

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);                        }    }

  欢迎大家多多交流。

  

原创粉丝点击