// Do not add any additional code to this method
private void InitializePhoneApplication()
{
if (phoneApplicationInitialized)
return;
// Create the frame but don't set it as RootVisual yet; this allows the splash
// screen to remain active until the application is ready to render.
RootFrame = new PhoneApplicationFrame();
RootFrame.Navigated += CompleteInitializePhoneApplication;
// Handle navigation failures
RootFrame.NavigationFailed += RootFrame_NavigationFailed;
// Ensure we don't initialize again
phoneApplicationInitialized = true;
}
可以看到,注释提到,不要添加对于的代码到该方法中,所以我们也不要这么做。
当我们改变某个Page的属性时不会影响其他页面,但是一旦改变了Frame,那就会影响所有的页面,所以,我们应该认为Frame是只读的(虽然不是)。
App类是程序主类,我没刻意用它来操作全局的属性,比如获取Frame,就是RootFrame属性。
页面导航的方法
页面导航一般分为两种方法,代码导航,和XAML声明导航
1、代码实现
NavigationService类提供了导航的功能,NavigationService.Navigate(new Uri("/NewPage.xaml", UriKind.Relative));将这段代码放到Button点击事件中,点击Button就会跳转到NewPage.xaml页面,其中naviagte接受一个Uri类型的参数,这里传入字符串路径和路径类型UriKind,UriKind是个枚举型,一边页面导航是相对路径。WP7的特点的从根目录起,"/"开头表示根目录,依次输入文件路径,NewPage.xaml文件放在了根目录下,所以路径写为"/NewPage.xaml", 如果NewPage.xaml在根目录的View文件夹下,则路径为"/View/NewPage.xaml"。
2、XAML实现
可以利用导航控件如 HyperlinkButton,写法,NavigateUri属性用来设置路径。
上述两种方式达到的目的是一样的,点击Button或HyperlinkButton就会跳转到NewPage.xaml
WP7页面导航可以使用路径别名,也是从Silverlight继承过来的
首先在App.xaml的文件上注册Windows.Navigation 命名空间,代码:mlns:navigate="clr-namespace:System.Windows.Navigation;assembly=Microsoft.Phone",
然后注册资源代码如下:
IsolatedStorageSettings iss = IsolatedStorageSettings.ApplicationSettings;
if (iss.Contains("ID"))
{
MessageBox.Show(iss["ID"].ToString());
}
最终运行和NavigationService跳转在路径后面加参数效果是一样的