【发布时间】:2014-04-15 12:25:59
【问题描述】:
我正在使用 VS 2013 开发一个简单的 WP8 应用程序。我有一个 MainPage.xaml 并添加了一个名为 Page1.xaml 的新页面。当用户单击“新项目”时,我在屏幕上有一个选项列表我想打开一个新页面来添加新项目(例如 Page1.xaml)。
在列表选择更改事件上,我编写了以下代码:
private void OptionssList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selection = (MenuItem) e.AddedItems[0];
switch (selection.Id)
{
case 0:
break;
case 1:
break;
case 2:
this.NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
break;
case 3:
break;
default:
break;
}
}
当我尝试调试应用程序时,我注意到如果我有一个 OnNavigatedTo 事件处理程序,则会调用 Page1.xaml 的构造函数,但是在这一切之后会引发 Unhandled 异常。抛出异常时我看不到任何代码,但是它调用了 Application_UnhandledException 事件处理程序。
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
if (Debugger.IsAttached)
{
// An unhandled exception has occurred; break into the debugger
Debugger.Break();
}
}
异常详情如下图:
System.NullReferenceException was unhandled
Message: An unhandled exception of type 'System.NullReferenceException' occurred in System.Windows.ni.dll
Additional information: Object reference not set to an instance of an object.
我想知道我是否遗漏了什么。我提到了示例,它也显示了与导航类似的方式,没有注意到任何花哨的东西。
【问题讨论】:
-
也许我们需要更多细节来了解会发生什么。您的 Page1.xaml 可能有问题,它是否包含特殊内容?
-
我的第一页没有什么特别的。我刚刚创建了一个新页面我还没有添加任何控件。
标签: xaml windows-phone-8 windows-phone navigationservice