Windows Phone 8.1 页面导航
1. Windows Phone 8.1 的应用框架

一个应用拥有 1 个 Window,一个 Window 包含 1 个 Frame,一个 Frame 包含 多个 Page。
获取 Frame 的方法为:
Frame rootFrame = Window.Current.Content as Frame;
用 Page 填充 Frame 的方法为:
rootFrame.Navigate(typeof(MainPage))
2. Windows Phone 8.1 与 Windows Phone 8.0 导航的不同
(1)“返回键”的默认行为是返回到上一个应用(没有上一个应用则返回桌面),并不是上一个页面。
若想要实现返回上一个页面的操作,则需要改写 HardwareButtons.BackPressed 事件:
public App()
{
this.InitializeComponent();
this.Suspending += this.OnSuspending; HardwareButtons.BackPressed += HardwareButtons_BackPressed;
} private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if( rootFrame != null && rootFrame.CanGoBack )
{
rootFrame.GoBack();
e.Handled = true;
}
}
或者只对某个 Page 改写:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
} private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
e.Handled = true; if( Frame.CanGoBack )
Frame.GoBack();
} protected override void OnNavigatedFrom(NavigationEventArgs e)
{
HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
}
(2)导航方法改为 Frame.Navigate。
导航时不再需要填写下一个页面的 Uri,而是直接使用页面的类型,并且可以直接将 Object 传递到下一个页面:
Frame.Navigate(typeof(Page2), "From MagePage.");
当然,这就需要下一个页面对传递过来的 Object 进行拆箱:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
myTextBlock.Text = (string)e.Parameter;
}
3. Frame.BackStack
应用可以获取之前的导航历史记录(不包括当前页面),Frame.BackStack 返回的是 IList<PageStackEntry>:
List<string> backStack = new List<string>();
foreach( var item in Frame.BackStack )
{
backStack.Add(item.SourcePageType.Name);
} myListView.ItemsSource = backStack;
可以轻松移除导航历史中的某项:
var backStack = Frame.BackStack;
backStack.RemoveAt(Frame.BackStackDepth - ); if( Frame.CanGoBack )
Frame.GoBack();
4. NavigationCacheMode
应用默认不对页面进行缓存,若要缓存当前页面则可以设置页面 NavigationCacheMode 属性:
public MainPage()
{
this.InitializeComponent(); this.NavigationCacheMode = NavigationCacheMode.Required;
}
同样可以更改 App.xaml.cs 里 OnLaunched 方法的 CacheSize:
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame; if( rootFrame == null )
{
rootFrame = new Frame(); // TODO: 将此值更改为适合您的应用程序的缓存大小
rootFrame.CacheSize = ; if( e.PreviousExecutionState == ApplicationExecutionState.Terminated )
{
} Window.Current.Content = rootFrame;
} if( rootFrame.Content == null )
{
if( rootFrame.ContentTransitions != null )
{
this.transitions = new TransitionCollection();
foreach( var c in rootFrame.ContentTransitions )
{
this.transitions.Add(c);
}
} rootFrame.ContentTransitions = null;
rootFrame.Navigated += this.RootFrame_FirstNavigated; if( !rootFrame.Navigate(typeof(MainPage), e.Arguments) )
{
throw new Exception("Failed to create initial page");
}
} Window.Current.Activate();
}
5. NavigationHelper
如果你新建的项目不是空白项目,则会在项目中发现一个 Common 文件夹,而该文件夹中会有一个 NavigationHelper 类。
该类会帮你解决应用的导航问题,使用方法:
(1) 在某处创建一个 NavigationHelper 实例(如页面的构造函数中),并注册 LoadState 和 SaveState 事件的回调。
public MyPage()
{
this.InitializeComponent();
var navigationHelper = new NavigationHelper(this);
this.navigationHelper.LoadState += navigationHelper_LoadState;
this.navigationHelper.SaveState += navigationHelper_SaveState;
} private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{ } private async void navigationHelper_SaveState(object sender, LoadStateEventArgs e)
{ }
(2) 在以下情况下注册页面以调入 NavigationHelper: 该页面通过重写 OnNavigatedTo 和 OnNavigatedFrom 事件以参与导航:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
navigationHelper.OnNavigatedTo(e);
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
navigationHelper.OnNavigatedFrom(e);
}
Windows Phone 8.1 页面导航的更多相关文章
- windows phone8.1:页面导航详解
小梦给大家带来windows phone 8.1应用开发实战教程,分享自己学习,开发过程中的经验和技巧. 今天给大家分享windows phone 8.1页面导航相关知识.涉及知识点如下: 页面一导航 ...
- Windows Phone 8.1 新特性 - 页面导航
本篇介绍一下Windows Phone 8.1 中页面导航的实现方式. 大家对Windows Phone 8 中页面导航的实现一定不陌生,我们使用 NavigationService 来实现.具体写法 ...
- 与众不同 windows phone (27) - Feature(特性)之搜索的可扩展性, 程序的生命周期和页面的生命周期, 页面导航, 系统状态栏
原文:与众不同 windows phone (27) - Feature(特性)之搜索的可扩展性, 程序的生命周期和页面的生命周期, 页面导航, 系统状态栏 [索引页][源码下载] 与众不同 wind ...
- windows phone 页面导航(6)
原文:windows phone 页面导航(6) 页面导航的例子我们使用的是两个页面,从第一个页面(MainPage)导航到第二个页面(SecondPage),然后可以从第二个页面导航到第一个页面 , ...
- WinPhone学习笔记(一)——页面导航与页面相关
最近学一下Windows Phone(接下来简称“WinPhone”)的开发,在很久很久前稍探究一下WinPhone中对一些传感器的开发,那么现在就从头来学学WinPhone的开发.先从WinPhon ...
- 【Win10】页面导航的实现
注:本文基于 Windows 10 10240 及其 SDK 编写,若以后有变化,请以新版本为准. 页面导航我们是再熟悉不过了,浏览器.手机 App 大多都使用这种方式来展示内容.在 Windows ...
- wp8.1 Study1: 页面导航&页面间值传递
摘要:wp8.1与wp8中很多API是不一样了,wp8.1把以前wp7.x时的api去掉了,更多与win8.1的API相似.比如以下的页面导航和页面之间的值传递 1.页面导航 利用Frame.Navi ...
- Win10系列:JavaScript页面导航
页面导航是在开发应用的过程中使用频率较高的技术,其中比较常用的导航方式有多页导航和页内导航,采用多页导航方式的应用程序包含一系列的页面,在一个页面中加入另一个页面的链接地址后,单击链接将跳转到指定页面 ...
- wp8.1 页面返回 页面导航
public The_second() public second() { this.InitializeComponent(); Frame frame = Window.Current.Conte ...
随机推荐
- Java 开源博客——B3log Solo 0.6.6 正式版公布了!
Java 开源博客 -- B3log Solo 0.6.6 正式版公布了!欢迎大家下载. 该版本号引入了数据库连接池:Druid. 另外,欢迎观摩 B3log 团队的新项目:Noty,也很欢迎大家參与 ...
- restful php
http://bbs.phpchina.com/thread-228725-1-1.html http://www.cnblogs.com/artech/p/3506553.html http://w ...
- SelectionKey理解(总结)
SelectKey注册了写事件,不在合适的时间去除掉,会一直触发写事件,因为写事件是代码触发的 client.register(selector, SelectionKey.OP_WRITE); 或者 ...
- [转]不用安装Oracle Client如何使用PLSQL Developer
本文转自:http://www.cnblogs.com/sleepywang/archive/2009/10/13/1582654.html 1. 下载oracle的客户端程序包(30M) 只需要在O ...
- Linux(CentOS)安装rar和unrar以及rar和unrar命令的使用
可以参考此篇博文. http://www.cnblogs.com/linjiqin/archive/2013/03/24/2979736.html 不过我按照其步骤手动安装Linux的rar文件执 ...
- Android开发需要注意的坑
Android开发需要注意的坑一览对于一些Android开发过程中坑爹.细小,但又重要的错误的总结Android开发在路上:少去踩坑,多走捷径其他参考: google官方版本发布图 umeng ...
- display:none与visible:hidden区别
if(list.style.display=='none'){ list.style.display='block'; }else{ ...
- 织梦dedecms源码安装方法
织梦dedecms源码安装方法 第一步: 上传所有文件到空间 注意:(由于有很多人反应安装后首页样式都乱的,所以强烈要求安装到根目录,如:127.0.0.1 / www.xxx.com,或者二级域名也 ...
- Java Concurrency - 浅析 Phaser 的用法
One of the most complex and powerful functionalities offered by the Java concurrency API is the abil ...
- MyBatis(3.2.3) - Configuring MyBatis using XML, typeAliases
In the SQL Mapper configuration file, we need to give the fully qualified name of the JavaBeans for ...