簡體   English   中英

無法將用戶導航到MainPage.xaml

[英]Can't navigate user to MainPage.xaml

我在這里真是個奇怪的情況。 為了簡單起見,讓我們說我有3頁。 MainPage.xaml ”,“ SeconPage.xaml ”和“ ThirdPage.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”中的完全相同的代碼進行完全相同的操作時。 一切運行正常...我不知道如何解決這個問題,特別是如果沒有錯誤消息出現時。 任何幫助將不勝感激!

編輯 (按@AmanKhandelwal的要求,為ThirdPage.xaml.cs)

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");
    }
}

好吧,好像您以錯誤的方式使用了導航器,正確的同步是

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

按照說明進行更改后,將其放入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM