【问题标题】:how to navigate to another xaml page in Windows Mobile如何导航到 Windows Mobile 中的另一个 xaml 页面
【发布时间】:2014-11-16 18:28:02
【问题描述】:

当单击当前 xaml 页面中的列表框项时,我正在尝试转到另一个 xaml 页面。我就是这样做的。

 private void mainList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (mainList.SelectedIndex == -1)
            return;

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

但它给了我“NULLREFERENCEEXCEPTION”错误。 groupID groupName 不为空。当我将最后一句话更改为 this.Content=new RegisterSurveyee() 时,它工作正常。问题仅发生在 NavigationService.Navigate(new Uri("/RegisterSurveyee.xaml",UriKind.Relative)); 有什么帮助???

【问题讨论】:

  • 您需要调试代码以查看空引用在何处引发异常。在方法中放置一个断点并逐步执行它。可能是添加的项目不是 SurveyWindowsPhone.Tables.SurveyGroup 类型,因此转换失败。我的预感是你设置组 ID 的地方。

标签: windows-phone-7


【解决方案1】:

i) 确保 e.AddedItems[0] 不为 null 且类型为 SurveyGroup

如果你得到空引用异常,

 if (mainList ==null || mainList.SelectedIndex == -1)
            return;

ii) 从一个页面导航到另一个页面时,您必须提供全名,包括 .xaml 扩展名

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

如果您仍然收到 NullReference 异常,请尝试在 Page 的 Loaded 事件上注册到 SelectionChanged 事件。

    private void MainPage_OnLoaded(object sender, RoutedEventArgs e)
    {
        ListBox.SelectionChanged += ListBoxSelectionChanged;
    }

【讨论】:

  • 确保您将类型转换为正确的类 SurveyGroup
  • 检查“mainList”是否是您的列表框的名称。(见编辑)
  • 两个名称相同。每个示例和教程都与我的相同,但我仍然收到此错误。现在,我正在使用 content=new RegisterSurveyee();
  • 你是否包括了空检查?
  • 我尝试导航到不同的 xaml 页面,但结果相同。我没有检查null。 NavigationService.Navigate(new Uri("/RegisterSurveyee.xaml",UriKind.Relative)) 发生错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多