【问题标题】:NavigationService.navigate null reference exceptionNavigationService.navigate 空引用异常
【发布时间】:2015-02-27 04:08:54
【问题描述】:

我正在学习 WP 编码,但遇到无法解决的问题:/

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

edit.xaml 与 MainPage.xaml 位于同一目录中

它抛出“NullReferenceException”

【问题讨论】:

  • 我猜你是从MainPage 调用这段代码的?你能告诉我们完整的调用堆栈吗? (只需复制/粘贴ex.ToString()的内容)

标签: c# windows-phone-7


【解决方案1】:

因为你得到一个NullReferenceException,我猜你正试图提前调用NavigationService.Navigate,例如在MainPage构造函数中。

相反,如果您想在页面加载时立即导航,请尝试通过将以下代码添加到 MainPage 类来覆盖 OnNavigatedTo 事件:

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

【讨论】:

    【解决方案2】:

    这样就解决了问题:

    (Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/edit.xaml", UriKind.Relative));
    

    【讨论】:

      【解决方案3】:

      使用这个.... 这解决了我的问题。

      this.Loaded += (a, b) => {my code}
      

      【讨论】:

      • 这对我有用,我只是想问一下这样做有什么影响吗?
      猜你喜欢
      • 2011-05-16
      • 1970-01-01
      • 2013-09-21
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多