LuckyCoder
LuckyCoder

Reputation: 520

Windows Phone 8 open new page programmatically

I've started WP 8 development recently. I know C# a bit but not much. BTW, I'm trying to open a page pragmatically, but app is crushing.

My code is here

NavigationService.Navigate(new Uri("/Dashboard.xaml", UriKind.Relative));

But I'm being confused because it's working when I'm placing the code above within button click event code block.

ERROR Detail An exception of type 'System.NullReferenceException' occurred in TestProgram.DLL but was not handled in user code

If there is a handler for this exception, the program may be safely continued.

I need your advice.

EDIT: Code Added

Credens MyCred = new Credens();

// Constructor
public MainPage()
{
    InitializeComponent();

    if (MyCred.ifExists("api_key"))
    {
        NavigationService.Navigate(new Uri("/Dashboard.xaml", UriKind.Relative));
    }

}

Upvotes: 0

Views: 3858

Answers (3)

Pantelis
Pantelis

Reputation: 2070

Try calling Navigate() on the PhoneApplicationPage Loaded or OnNavigatedTo() events.

Upvotes: 0

Igor Kulman
Igor Kulman

Reputation: 16369

You cannot use the NavigationService in the constructor. Put your code to the OnNavigatedTo event and it will not crash

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if (MyCred.ifExists("api_key"))
    {
        NavigationService.Navigate(new Uri("/Dashboard.xaml", UriKind.Relative));
    }
}

Upvotes: 6

aloisdg
aloisdg

Reputation: 23551

Did you follow this tutorial step by step ?

You code seem right. As you said, you should have something like this :

private void hyperlinkButton1_Click(object sender, RoutedEventArgs e)
{
    NavigationService.Navigate(new Uri("/Dashboard.xaml", UriKind.Relative));
}

Does you page are on the same folder ? Did you check the path ? Does you page exist ? Can you boot in it ? If you add a break point on the NavigationService, where failed it ?

I think this documentation is pretty helpful.

Upvotes: 1

Related Questions