2013-12-13 65 views
0

我在Windows Phone应用程序中有一个页面。在这个页面有一些时间。用户可以通过按下删除按钮来删除这些项目。以前,当按下后退按钮时,用户可能会看到已删除的项目,并导致程序出错。所以我重写后退按钮,并停止它的作品。我在我的应用上创建了自己的后退按钮,当用户按下我应用的后退按钮时,它将导航到主页面。 我用这个代码:导航到新页面失败

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e) 
    { 
     e.Cancel = true; //Cancels the default behavior. 

    } 

    private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     NavigationService.Navigate(new Uri("/MainPage_page.xaml", UriKind.Relative)); 
    } 

但导航主页无法正常工作。我该如何解决这个问题?

是否有可能通过按下手机的返回键这样的导航到主页:

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e) 
    { 
     e.Cancel = true; //Cancels the default behavior. 
     NavigationService.Navigate(new Uri("/MainPage_page.xaml", UriKind.Relative)); 

    } 

回答

0

变化:

NavigationService.Navigate(new Uri("/MainPage_page.xaml", UriKind.Relative)); 

收件人:

NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative)); 

除非你已经重命名页面当然。

0

这可以帮助你。

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e) 
    { 
     // e.Cancel = true; 
     NavigationService.Navigate(new Uri("/MainPage_page.xaml", UriKind.Relative)); 

    } 

您的按钮单击事件似乎没问题导航到主页没有问题。

//似乎确定

private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     NavigationService.Navigate(new Uri("/MainPage_page.xaml", UriKind.Relative)); 
    } 

如果我猜的没错还有一些其他的导航在MainPage_page.xaml的OnNavigatedTo方法

//查看您的MainPage_page.xaml

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
} 
+0

如果我在OnBackKeyPress使用导航代码则错误显示了在该://代码以如果导航失败 私人无效RootFrame_NavigationFailed(对象发件人,NavigationFailedEventArgs E) { 如果(System.Diagnostics.Debugger.IsAttached执行) { //导航失败;打入调试器 System.Diagnostics.Debugger.Break(); } } – DarkenShooter

+1

寻找异常,我会建议你检查你的页面名称,是“/MainPage_page.xaml”的正确路径吗?当找不到相对路径时,会发生此异常。 – Jaihind