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 页面导航的更多相关文章

  1. windows phone8.1:页面导航详解

    小梦给大家带来windows phone 8.1应用开发实战教程,分享自己学习,开发过程中的经验和技巧. 今天给大家分享windows phone 8.1页面导航相关知识.涉及知识点如下: 页面一导航 ...

  2. Windows Phone 8.1 新特性 - 页面导航

    本篇介绍一下Windows Phone 8.1 中页面导航的实现方式. 大家对Windows Phone 8 中页面导航的实现一定不陌生,我们使用 NavigationService 来实现.具体写法 ...

  3. 与众不同 windows phone (27) - Feature(特性)之搜索的可扩展性, 程序的生命周期和页面的生命周期, 页面导航, 系统状态栏

    原文:与众不同 windows phone (27) - Feature(特性)之搜索的可扩展性, 程序的生命周期和页面的生命周期, 页面导航, 系统状态栏 [索引页][源码下载] 与众不同 wind ...

  4. windows phone 页面导航(6)

    原文:windows phone 页面导航(6) 页面导航的例子我们使用的是两个页面,从第一个页面(MainPage)导航到第二个页面(SecondPage),然后可以从第二个页面导航到第一个页面 , ...

  5. WinPhone学习笔记(一)——页面导航与页面相关

    最近学一下Windows Phone(接下来简称“WinPhone”)的开发,在很久很久前稍探究一下WinPhone中对一些传感器的开发,那么现在就从头来学学WinPhone的开发.先从WinPhon ...

  6. 【Win10】页面导航的实现

    注:本文基于 Windows 10 10240 及其 SDK 编写,若以后有变化,请以新版本为准. 页面导航我们是再熟悉不过了,浏览器.手机 App 大多都使用这种方式来展示内容.在 Windows ...

  7. wp8.1 Study1: 页面导航&页面间值传递

    摘要:wp8.1与wp8中很多API是不一样了,wp8.1把以前wp7.x时的api去掉了,更多与win8.1的API相似.比如以下的页面导航和页面之间的值传递 1.页面导航 利用Frame.Navi ...

  8. Win10系列:JavaScript页面导航

    页面导航是在开发应用的过程中使用频率较高的技术,其中比较常用的导航方式有多页导航和页内导航,采用多页导航方式的应用程序包含一系列的页面,在一个页面中加入另一个页面的链接地址后,单击链接将跳转到指定页面 ...

  9. wp8.1 页面返回 页面导航

    public The_second() public second() { this.InitializeComponent(); Frame frame = Window.Current.Conte ...

随机推荐

  1. [Angular 2] Start with Angular2

    Create a index.html: <!DOCTYPE html> <html> <head> <title>Really Understandi ...

  2. ViewGroup源码部分解析

    ViewGroup的官方解析是: A <code>ViewGroup</code> is a special view that can contain other views ...

  3. sqlserver 日期相关2

    1.常用日期方法(下面的GetDate() = '2006-11-08 13:37:56.233') (1)DATENAME ( datepart ,date ) 返回表示指定日期的指定日期部分的字符 ...

  4. A Simple Actions Recognition System

    1. Problem Definition There's no doubt that researches and applications on the foundation of videos ...

  5. java_log4j多文件配置

    今天配置了log4j中写多个文件的内容,配置了半天才搞出来,为了避免类似问题,写个博客吧. 首先说一下需求,每天要在7个文件夹中生成文件,文件格式为xxx.log.2000.01.01,自己开发个写文 ...

  6. eclipse中,把java函数代码折叠/展开

    首先,在eclipse 中开启设置代码折叠功能 1. windows->perferences->General->Editors->Structured Text Edito ...

  7. python实现之极简stack和queue

    用python实现一个极简的stack和queue,那是so easy的事情了,简洁易懂,适合小白~ 直接上代码吧: node: class LinkNode: def __init__( self, ...

  8. Scala Java Error: value filter is not a member of *

    有时在Scala中调用Java的库,Java库会返回某些Java的集合或类型,必须经过一些转换才能正常使用. 否则有可能在编译的过程遇到这个错误. 错误字符串 下面是错误的主要信息. Scala Ja ...

  9. eclipse按Crl+鼠标左键,找不到源文件的解决办法。

    这种情况一般发生在tomcat的之中,原因是缺少类的源文件.在jdk中很少见,jdk中自带类的源文件,配置jdk的时候就已经将其加载进来了.而tomcat之中没有带类的源文件,需要自己去网上单独下载. ...

  10. 1. Android 系统上一款开源的图表库

    1. MPAndroidChart  MPAndroidChart 是 Android 系统上一款开源的图表库.目前提供线图和饼图,支持选择.缩放和拖放. 一个可以拖动缩放的图表库,包含曲线图.直方图 ...