本文介绍了NavigationService.navigate空引用异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习WP编码和我有解决不了的问题:/

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

try
 {
    NavigationService.Navigate(new Uri("/edit.xaml", UriKind.Relative));
 }
 catch (Exception ex)
 {
     MessageBox.Show(ex.Message.ToString(),"Error!",MessageBoxButton.OK);
 }



edit.xaml在同一目录MainPage.xaml中

edit.xaml is in the same directory as MainPage.xaml

这抛出的NullReferenceException

It throws "NullReferenceException"

推荐答案

由于你得到一个的NullReferenceException ,我的猜测是,你正在试图调用 NavigationService.Navigate 来早,例如在构造函数中的MainPage

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

相反,如果你想immedietly导航时,页面加载例如,尝试通过将此代码重写的OnNavigatedTo 事件做到MainPage类:

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

这篇关于NavigationService.navigate空引用异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 19:38