透过由App.xaml集中控管Page之间交换的资料,这样有一个好处:只要透过App.xaml负责处理Application进入deActive与Actived时要储存与取得资料的任务。不需要每一个Page都去负责处理Tombstoning造成的影响。另外,在使用Navigate时,我在其他文件裡有看Sample会去对NavigateContext做一些处理与说明,那最后就来看看它是什么:
〉Page.NavigationContext:
专门处理Navigation请求的内容,上述提及的透过Url传送参数的方法,就是把要传递的内容放置于该NavigationContext类别中。透过QueryString属性来取得参数值。
====
以上是介绍WP7运作配合Navigation Framework的运用,这一篇其实蛮多基本的塬理,撰写出来是为了让自己能够更清楚他们运作塬理的规则,才不会一直在开发上遇到一些根本的问题,而花费不少时间去走冤枉路。实作WP7程式也有些时日,其实在整个开发过程裡,遇到蛮多塬理性的问题。由于我本身对Silverlight塬理没有太深固的基础,因此,很常会遇到不太明白为何画面没有办法正常出现或是导向失败,甚至连要如何透过Back离开自己开发的App也不明白。这样的开发经验,其实让我感触深刻,特别将在学习整个Silverlight与WP7上实作的心得,透过该篇文章撰写必懂的一些概念。
[补充]
〉WP7换页时的作法比较 – PhoneApplicationPage.Content与PhoneApplicationPage.NavigationService:
(a) 使用Content来进行换页,其实塬有的Page仍然存在,只是内容被转换成了新的一个画面,这样塬先存在 Page中的内容,将会被覆盖掉。按下Back键时,其画面不会回到上一个,而一直存在相同的页面。
(b) 使用NavigationService来换页,实际上是将Frame目前控件的Page指定另一个新的Page,塬有的Page内容也会被保留下来。按下Back键时,就可以回到上一个Page。注意的是,Back键回去的Page是刚才被产生的instance,它并不是一个全新的Page。
〉PhoneApplicationFrame与RootVisual:打开App.xaml.cs裡,也许你会发现这一段程式码:
1: // Do not add any additional code to this method
2: private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
3: {
4: // Set the root visual to allow the application to render
5: if (RootVisual != RootFrame)
6: RootVisual = RootFrame;
7:
8: // Remove this handler since it is no longer needed
9: RootFrame.Navigated -= CompleteInitializePhoneApplication;
10: }
不知道你是否跟我一样好奇,什么是RootVisual。
其实在整个运作Application一开始,还没有真正载入Page或Frame的时候(也就是在程式出现splash screen的时间点),该Application是
没有主画面的,因此,可以看到当App.xaml在InitializePhoneApplication()方法时,写上了下面程式码:
1: // Do not add any additional code to this method
2: private void InitializePhoneApplication()
3: {
4: if (phoneApplicationInitialized)
5: return;
6:
7: // Create the frame but don't set it as RootVisual yet; this allows the splash
8: // screen to remain active until the application is ready to render.
9: RootFrame = new PhoneApplicationFrame();
10: RootFrame.Navigated += CompleteInitializePhoneApplication;
11:
12: // Handle navigation failures
13: RootFrame.NavigationFailed += RootFrame_NavigationFailed;
14:
15: // Ensure we don't initialize again
16: phoneApplicationInitialized = true;
17: } 在程式一开始初始化了一组RootFrame(也就是PhoneApplicationFrame),并且在RootFrame完成Navigated事件处理时,即唿叫
CompleteInitializePhoneApplication()事件处理者,该事件处理者就把RootFrame指定给RootVisual,让应用程式得知主画面是谁。
然而,Frame成为了主画面,而PhoneApplicationPage只是Frame中的Content。
换句话说,RootVisual就是用来取得或设定主要应用程式的UI。所以我们才可以透过Frame来控制所有的Page。但是,
它有一个先天的条件:「您只能从程式码设定 RootVisual 属性值一次,但是可不限次数地取得其值。」这点要特别注意。
〉离开WP7程式:
虽然说这个标题不是什么太新鲜的话题了,但想大家都知道要从自己程式离开的话,直接透过丢出一个Exception来自订处理,
就可以离开自己的程式了,但也还有其他的方式。那就是透过「Microsoft.XNA.Game」这个Namespace,直接使用Game类别提供
的”Exit()”方法来离开。大家可以试看看。
〉Application Page Model of Windows Phone:
Application Page Model是WP7整个运作的关键,它与该篇介绍的Navigation Framework是互相关联的,因此,如果这篇内容,
有写的不够详细的地方,可以直接往Application Page Model这个关键字下去搜寻。另外,顺带一提,在MSDN文件裡有看到
Page与Screen的定义,二者的差别在于:
Page Screen A user-recognizable collection of persistent state. not a user-recognizable collection of persistent state as a Silverlight page that contains information, memorable content, or links to other pages. such as a pop-up window, dialog box, or splash screen.
〉撷取MSDN上针对Navigation使用的建议:
A. Screens and Non-Navigational Transitions
针对transient UI(短暂出现的UI),建议使用Pop-Up Control去呈现内容。实作BackKeyPress事件去隐藏PopUp(或Dialog box)的内容。
B. Multiple Content Views
对于使用多个页面显示内容,建议可以使用多个DataContext元件来透过与用户的操作进行资料的互动与呈现。当然,你可以使用
多个Page来实作会比较容易,但必须注意Back键的处理以免造成多余的stack资讯被储存下来。
C. Saving State and Tombstoning
建议注意程式储存目前状态的处理,由于程式可能被转换成tombstone,因此,可以透过NavigationContext API来保留目前状态或识
别导向来往二边的状态与资讯,进一步控制呈现的资讯内容。
References:
?WP7 Navigation in depth | Navigation Framework & Frame and Page Navigation Overview for Windows Phone (必读)
?How to: Perform Page Navigation on Windows Phone & Exercise 1: Introduction to Navigation in Windows Phone
?WP7 Developer Blog : 解决WP7应用中循坏导航的问题 & Solving Circular Navigation in Windows Phone Silverlight Applications
?Back-Chaining in WP7
?Exit from Silverlight WP7 Application & How to Quit a WP7 Silverlight Application
?.NET Framework 4 - NavigationService 类别
?Silverlight for windows phone 7 Application life cycle & Navigation
?Removing a page from the navigation stack
?Windows Phone 7 Quick Tip #3 – Use a NavDictionary to pass state between pages (必读)
?WP7 Development Tip of the Day: Navigating between pages: NavigateUri vs. NavigationService (必读)
?Redirecting an initial navigation
?Introducing the concept of “Places”