NavigationService.Navigate抛出异常并在WP8中调用Application_UnhandledException

时间:2013-10-20 07:02:05

标签: xaml windows-phone-8 windows-phone navigationservice

我正在使用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的构造函数,但是在抛出所有这些之后会抛出一个未处理的异常。抛出异常时我没有看到任何代码,但它会调用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.

我想知道我是否遗漏了什么。我提到的样本它也显示了与Navigate类似的方式,没有注意到任何花哨的东西。

1 个答案:

答案 0 :(得分:0)

我知道这可能是一个非常愚蠢的问题,你的代码对我来说很好,你的页面可能不在另一个文件夹中吗?我总是把我的页面放在一个名为UI的文件夹中,所以我的代码看起来像这样:

                                         /folder1/folder2/pagename
this.NavigationService.Navigate(new Uri("/UI/Generics/Page.xaml", UriKind.Relative));

确保这一点,因为这通常会让我。