簡體   English   中英

WP7導航 - NullReferenceException

[英]WP7 Navigation - NullReferenceException

我需要在第一次運行我的應用程序時導航到某個頁面,以收集登錄詳細信息等。我正在使用IsloatedStorageSettings來保存值以確定這是否是應用程序的第一次運行,這樣可以正常工作。

我的問題實際上是在第一次運行應用程序時導航到我的“第一次運行”頁面,使用NavigationService,此時似乎沒有創建NavigationService,所以仍然為null。 什么時候創建NavigationService或者我該如何解決這個問題?

我的代碼(在我的主頁的構造函數中:

if ((bool)settings["firstRun"])
 { 
    if (NavigationService != null)
    {
        NavigationService.Navigate(new Uri("/FirstRun.xaml", UriKind.Relative));
    }
    else
    {
        MessageBox.Show("Navigation service must be null?");   //always prompts
    }                
 }
else
{
   InitializeComponent();
} 

Peter Torr有一篇很棒的博客文章,介紹了重定向初始導航的細節,但對於用戶登錄,我建議您使用全屏彈出窗口或在“普通”起始頁面上設置登錄控件並切換可見性根據您的第一次運行條件。

在課堂上添加

    private bool m_onNavigatedToCalled = false;

在ctor

   this.LayoutUpdated += new EventHandler(MainPage_LayoutUpdated);

然后在代碼中

    void MainPage_LayoutUpdated(object sender, EventArgs e)
    {
        if (m_onNavigatedToCalled)
        {
            m_onNavigatedToCalled = false;
            Dispatcher.BeginInvoke(() =>
            {
                if (NavigationService != null)
                {
                    MessageBox.Show("Navigation not null?"); //always prompts
                }
                else
                {
                    MessageBox.Show("Navigation service must be null?");   
                } 
                //StartApp(); do all stuff here to keep the ctor lightweight
            }
            );
        }
    }

暫無
暫無

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

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