Windows Phone 7页面导航的方法
在继承PhoneApplicationPage的类中可以使用NavigationService提供的导航方法Navigate,即代码如下:
this.NavigationService.Navigate(new Uri("/newPageName.xaml ", UriKind.Relative));
注意第二个参数是UriKind.Relative
NavigationService.Navigate Method
Navigates to the content specified by the uniform resource identifier (URI).
Namespace: System.Windows.Navigation
Assembly: Microsoft.Phone (in Microsoft.Phone.dll)
Syntax
public bool Navigate(
Uri source
)
Parameters
source
Type: System.Uri
The URI of the content to navigate to.
Return Value
Type: System.Boolean
Returns Boolean. True if the navigation started successfully; otherwise, false.
示例说明
我们以MainPage中点击鼠标跳转到另一个页面为例说明
在MainPage中添加button按钮
private void button1_Click(object sender, RoutedEventArgs e)
{
bool bTrans = this.NavigationService.Navigate(new Uri("/Page111.xaml?name=aaaa",UriKind.Relative));
if (!bTrans)
{
this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
} 单步调试发现,NavigationService.Navigate导航到不存在页面Page111.xaml时,其返回值也为True。
据此了解,通过返回值判断跳转是否成功的方法不可取。仔细看看上一节中Navigate返回值的说明:True if the navigation started successfully。明白了,原来Navigation启动成功了,其返回值即为True,只是没有找到Page而已。
2. 考虑是捕获NavigationService.NavigationFailed Event处理页面跳转异常的处理
PhoneApplicationPage Members没有包含NavigationFailed事件处理,因此使用PhoneApplicationFrame Members的NavigationFailed事件处理。
本例中在App.xaml.cs中找到RootFrame_NavigationFailed方法,此方法就是本例中PhoneApplicationFrame捕获NavigationService.NavigationFailed Event处理页面跳转异常的处理。
RootFrame是Windows Phone 7--Silverlight应用程序的根窗体(Frame),在此根窗体下silverlight的Page页被调用。
RootFrame被定义在App.xaml.cs中,如图
原来的代码如下:
// Code to execute if a navigation fails
private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
if (System.Diagnostics.Debugger.IsAttached)
{
// A navigation has failed; break into the debugger
System.Diagnostics.Debugger.Break();
}
}
修改为:
// Code to execute if a navigation fails
private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
if (System.Diagnostics.Debugger.IsAttached)
{
// A navigation has failed; break into the debugger
System.Diagnostics.Debugger.Break();
}
// Code to execute if a navigation fails
private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
if (System.Diagnostics.Debugger.IsAttached)
{
// A navigation has failed; break into the debugger
System.Diagnostics.Debugger.Break();
}