2014-01-30 22 views
0

这是一个非常奇怪的情况,我在这里。为了简单起见,让我说我有3页。 'MainPage.xaml','SeconPage.xaml'和'ThirdPage.xaml'。我试图导航到“MainPage.xaml中“ThirdPage.xaml”使用下面的代码:无法将用户导航到MainPage.xaml

// Forward user to MainPage       
try 
{ 
    NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative)); 
} 
catch (Exception ete) 
{ 
    MessageBox.Show("Error when navigating: " + ete.ToString()); 
} 

这对于一些未知的原因,不能正常工作......没有错误被抛出,什么都没有。它什么都不做。而且这段代码确实被执行了,我已经用它的一堆MessageBox调用进行了确认。

当我尝试使用'SecondPage.xaml'中完全相同的代码完全相同时。一切工作完美...我不知道如何解决这个问题,特别是如果没有错误消息。任何帮助真的会很感激!

编辑 (ThirdPage.xaml.cs由@AmanKhandelwal要求)

public partial class ThirdPage : PhoneApplicationPage 
{ 
    public ThirdPage() 
    { 
     InitializeComponent(); 
    } 

    private void btnForgotPin_Click(object sender, RoutedEventArgs e) 
    { 
     // Clear out entire cache 
     DataCache myCache = new DataCache(); 
     myCache.deleteEntireCache(); 

     // Forward user to login screen       
     try 
     { 
      NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative)); 
     } 
     catch (Exception ete) 
     { 
      MessageBox.Show("Error when navigating: " + ete.ToString()); 
     } 

    } 

    protected override void OnBackKeyPress(CancelEventArgs e) 
    { 
     /* When back button is pressed on this page the app has to close, 
     * the user is not allowed to interact with the app without providing 
     * the authentication pin. 
     */ 
     throw new ExitException(); 
    } 

    protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e) 
    { 
     base.OnNavigatedFrom(e); 

     MessageBox.Show("Hitting OnNavigatedFrom"); 
    } 
} 
+1

我真的不知道问题是什么。但您尝试在ThirdPage.xaml中创建一个OnNavigatedFrom事件,并在MainPage.xaml.this中创建一个OnNavigatedTo,这只是检查/调试导航是否正确执行的一个步骤。 –

+0

是您在某个文件夹或主目录中的主页? –

+0

只是为了确保 - 做第二页和第三页不同(任何)或只是一个不同名称的副本?对于第二页和第三页,您从主导航到第三导航?我假设他们在同一个目录中 - 是的? – Romasz

回答

0

以及它好像你正在使用错误的方法正确了Syntex是

NavigationService.Navigate(new Uri("/[name_of_your_project];component/[page_name.xaml]", UriKind.Relative)); 
导航

在做了如上所述的更改之后加入此项。