2014-01-30 27 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)); 
導航

在做了如上所述的更改之後加入此項。