2014-09-29 88 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

相關問題