WP7 Navigation - NullReferenceException

I need to go to a specific page for the first time my application starts up to collect login information, etc. I use IsloatedStorageSettings to save the value to determine if this is the first application launch or not, which works well.

My problem is actually navigating on my “first launch” page, when the application is launched for the first time using the NavigationService, it seems that the NavigationService is not being created at this point so still empty. When is a NavigationService created or how can I get around this?

My code (in the constructor of my main page:

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();
} 
+3
source share
2 answers

, , , "" .

+6

    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
            }
            );
        }
    }
+2

Source: https://habr.com/ru/post/1785459/


All Articles