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. 关于IE8中使用Jquery load方法无法正常加载页面

    最近发现,在IE8中使用Jquery load方法时无法正常加载页面,页面显示空白,没有加载.调试发现,页面多了一个</div>标签,但在FF和CH下表现正常.希望能给遇到同样问题的码农有 ...

  2. 关于jquery ID选择器的一点看法

    最近看到一道前端面试题: 请优化selector写法:$(".foo div#bar:eq(0)") 我给出的答案会是: 1. $("#bar") 2.  $( ...

  3. Ejection chain 与交错路

    相关文献: Rego, C. (1998). "A Subpath Ejection Method for the Vehicle Routing Problem." Manage ...

  4. Getting Started with Zend Framework MVC Applications

    Getting Started with Zend Framework MVC Applications This tutorial is intended to give an introducti ...

  5. Windows 7中使用Eclipse 使用CDT and WinGW 开发C/C++(转载)

    以前使用visual studio 2010编写C/C++,后来接触了Eclipse后,据说eclipse也可以编写C/C++,以前觉得Visual studio 2010还蛮不错的,也没有多大好奇心 ...

  6. android scrollview主要的问题

    项目做多了之后,会发现其实 ScrollView嵌套ListVew或者GridView等很常用,但是你也会发现各种奇怪问题产生.根据个人经验现在列出常见问题以及代码最少最简单的解决方法. 问题一 :  ...

  7. python(4)-迭代器 和 生成器

    迭代器是访问集合元素的一种方式.迭代器适合遍历一些巨大或无限的集合,比如几个G的文件.迭代器具有以下特点: 1. 访问者不需要关心迭代器内部的结构,只需通过__next__()方法不断取下一个内容 2 ...

  8. UE设置 去掉bak备份文件

    使用ue打开文件,修改保存后,会产生.bak备份文件,感觉不爽,如何去掉呢? 1:在ue菜单栏,选择“高级”按钮选项  —— “配置”选项 2:在弹出的选择框中,找到“备份”—— 勾选“不备份” 选项 ...

  9. Oracle 追踪回话SQL几种方法

    生成sql trace可以有以下几种方式: 1.参数设置:非常传统的方法. 系统级别: 参数文件中指定: sql_trace=true 或 SQL> alter system set sql_t ...

  10. hdu 4666 最大曼哈顿距离

    思路:这题我是看了题目后,上百度搜了一下才知道还有求最大曼哈顿距离的方法.直接把代码copy过来,研读一下,知道了代码实现机制,自然就很容易想到用优先队列来维护每种状态下的xi,yi之和的最大值最小值 ...