【问题标题】:NullReferenceException when navigate from Class1.cs to Page1.xaml in Windows Phone 8在 Windows Phone 8 中从 Class1.cs 导航到 Page1.xaml 时出现 NullReferenceException
【发布时间】:2014-11-23 20:52:48
【问题描述】:

我需要从 Class1.cs 导航到 Page1.xaml。

我在 MainPage.cs 上尝试过这种方式

public void test()
{
    NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
}

并在 Class1.cs 中访问它:

...
MainPage window = new MainPage();
window.test();
....

但我明白了:

"[System.NullReferenceException]
{System.NullReferenceException: Object reference not set to an
instance of an object."

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

已经读过这个:How to navigate to a xaml page from class,但是没有用。

任何帮助我该怎么做?

PS:Class1.cs 和 Page1.cs 有不同的命名空间,如果重要的话。

【问题讨论】:

    标签: windows-phone-8 nullreferenceexception


    【解决方案1】:

    您不应该以这种方式实例化页面。相反,您可以使用PhoneApplicationFrame 公开的Navigate 方法:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多