0
votes

I create a very simple project to test navigation. Below are the steps.

  1. Create a Blank App (XAML/C#) project.
  2. Add a Basic Page "PageTwo" to the project.
  3. Add a HyperlinkButton and a TextBox to the MainPage.
  4. In the code-behind of the MainPage, using Frame.Navigate method to navigate to PageTow and pass the TextBox's Text as the parameter.
  5. Override OnNavigatedTo method of the PageTwo to get the passed parameter.

Run the project, input some text to the TextBox and click button to PageTwo, it works well, but if I click the built-in Back Button from PageTwo, I get an exception: Value cannot be null. If I comment the override OnNavigatedTo method, the Back button can lead me to the Main Page without exception.

Anyone can help?

MainPage.xaml:

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel>
        <TextBox Width="200" Name="TB"/>
        <HyperlinkButton Content="Go to PageTwo" Click="HyperlinkButton_Click_1"/>
    </StackPanel> 
</Grid>

MainPage.xaml.cs:

private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
    {
        Frame.Navigate(typeof(PageTwo), TB.Text);
    }

PageTwo.xaml:

<TextBlock Name="TB" Grid.Row="1"/>

PageTwo.xaml.cs:

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        TB.Text = e.Parameter as string;
    }
1
It's not my homework... - James
Sorry, can you show us your code? - Dmitry Dovgopoly
I have added the code - James
Can you upload project on SkyDrive or Dropbox, so I can inspect the problem as I can't regenerate your problem. - Farhan Ghumra

1 Answers

1
votes

In general when overriding any of the UI methods, you need to also call the base.

Your code does not cause an exception if I change the PageTwo.xaml.cs override of OnNavigatedTo to the following:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    TB.Text = e.Parameter as string;
    // call base method
    base.OnNavigatedTo(e);
}