【问题标题】:NavigationService.Navigate throws an exception and invokes Application_UnhandledException in WP8NavigationService.Navigate 抛出异常并在 WP8 中调用 Application_UnhandledException
【发布时间】: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


【解决方案1】:

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

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

请确保这一点,因为这通常会吸引我。

【讨论】:

  • 我的两个页面都在根文件夹中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-08
  • 1970-01-01
  • 2016-07-10
  • 2012-01-24
相关资源
最近更新 更多