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); } }
欢迎大家多多交流。
- win8页面导航--删除当前页面堆栈信息
- ViewPager删除当前页面
- 二级导航页面+当前页面导航高亮显示
- JS获取当前页面页面URL信息
- 导航条点击当前页面变色
- 在导航页面中获取当前viewController
- 切换导航标签实现当前标签颜色改变以及利用js如何准确获取当前页面url网址信息
- win8提示当前页面的脚本发生错误如何解决
- 页面导航
- 页面导航
- 判断当前页面的请求信息
- JQUERY获取当前页面的URL信息
- JQUERY获取当前页面的URL信息
- JQUERY获取当前页面的URL信息
- JQUERY获取当前页面的URL信息
- JQUERY获取当前页面的URL信息
- JQUERY获取当前页面的URL信息
- JS获取当前页面的URL信息
- 十步法将递归程序快速转变成迭代或循环程序
- C中打印堆栈或者通过函数指针获取函数名
- c语言作业
- 视图渲染
- 锐捷认证 无线wifi共享网络 一个账号 多人使用 呵呵
- win8页面导航--删除当前页面堆栈信息
- 替换非字母数字汉字空格外其他字符的SQL语句
- .net开发开源平台构建
- 菜鸟级别 ,请问:JAVA 从哪里入门
- Server.MapPath()
- Spring事务配置的五种方式
- 标准库— 操作源码之收集go包信息:go/build
- TreeSet练习
- Java知识点