2014-09-29 60 views
0

我需要从Class1.cs导航到Page1.xaml。在Windows Phone 8中从Class1.cs导航到Page1.xaml时导致NullReferenceException异常

我尝试中Class1上MainPage.cs

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

,并获得这样说:

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

,但我得到:

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

(新的Uri(“/ Page1.xaml”,UriKind.Relative));

已经阅读:How to navigate to a xaml page from class,但没有奏效。

任何帮助我该怎么做?

PS:Class1.cs和Page1.cs具有不同的名称空间,如果它很重要。

回答

1

你不应该这样实例化一个页面。相反,你可以使用PhoneApplicationFrame公开的Navigate方法:

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

它的工作!非常感谢!! – 2014-09-29 13:18:43