I have a issue where a windows phone 8 app, crashes on the this.NavigationService.Navigate(new Uri("/Dashboard.xaml", UriKind.Relative)); line as it trys to navigate to a new page. What the app should is load onto the welcome page, which is the one below, check to see if it is the first time the user has opened the app if so it should stay on that page until the user clicks the button to continue. But if it is not the first time the user has opened the app, it should check and then go straight to the dashboard. But the error is here, it does not want to navigate as it shows the error below. I have looked through all the other posts on this error message but no answer help this current situation.
This is the error message given;
An exception of type 'System.NullReferenceException' occurred in Good Morning Dashboard.DLL but was not handled in user code. Additional information: Object reference not set to an instance of an object. If there is a handler for this exception, the program may be safely continued.
This is the code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Good_Morning_Dashboard.Resources;
using System.IO.IsolatedStorage;
namespace Good_Morning_Dashboard
{
public partial class MainPage : PhoneApplicationPage
{
public bool trueOrFalse;
public string result;
public MainPage()
{
InitializeComponent();
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
if (!settings.Contains("DataKey"))
{
settings.Add("DataKey", "First Time");
}
else
{
settings["DataKey"] = "Not First Time";
this.NavigationService.Navigate(new Uri("/Dashboard.xaml", UriKind.Relative));
}
settings.Save();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.NavigationService.Navigate(new Uri("/Dashboard.xaml", UriKind.Relative));
}
}
}
Thank you in advance! :)
There are limitations to where you can use NavigationService
. I believe that you can't use it in a page constructor, as it will not be ready by then. You probably want to put it in the Page.OnNavigatedTo
override instead:
public MainPage()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
if (!settings.Contains("DataKey"))
{
settings.Add("DataKey", "First Time");
}
else
{
settings["DataKey"] = "Not First Time";
this.NavigationService.Navigate(new Uri("/Dashboard.xaml", UriKind.Relative));
}
settings.Save();
}