NavigationService.navigate null reference exception

后端 未结 3 834
名媛妹妹
名媛妹妹 2020-12-06 23:48

I\'m learning WP coding and I have problem that I can\'t solve :/

try
 {
    NavigationService.Navigate(new Uri(\"/edit.xaml\", UriKind.Relative));
 }
 catch         


        
3条回答
  •  难免孤独
    2020-12-07 00:24

    Because you get an NullReferenceException, my guess is that you are trying to call NavigationService.Navigate to early, for example in the MainPage constructor.

    Instead, if you want to navigate immedietly when page is loaded for example, try doing it by overriding the OnNavigatedTo event by adding this code to the MainPage class:

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
       NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
    }
    

提交回复
热议问题