2011-07-26 49 views
3
using System; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Navigation; 

namespace WindowsPhoneApplication7 
{ 
    public partial class Listbox : UserControl 
    { 
     public Listbox() 
     { 
      InitializeComponent(); 
     } 

     private void listbox(object sender, MouseEventArgs e) 
     { 
      this.NavigationService.Navigate(new Uri("/home.xaml", UriKind.Relative)); 
     } 
    } 
} 

錯誤是發生....... 不包含關於「的NavigationService」的定義和沒有擴展方法「的NavigationService」接受第一個參數發生類型「可以找到(是否缺少using指令或程序集引用?)錯誤是,當我們使用導航,移動其他頁面

回答

1

NavigationService是在PhoneApplicationPage類的屬性。你不是來自那個班,你是從UserControl派生。

您需要獲取用戶控件所在的父級電話頁面,然後從那裏獲取NavigationService參考。

編譯器錯誤是因爲它找不到Listbox類中NavigationService的定義。

0

Adam說的是正確的。但是,一個簡單的解決方案是定義按照App.xaml.cs靜態實用方法

public static PhoneApplicationFrame CurrentRootVisual 
{ 
    get 
    { 
     return (App.Current.RootVisual as PhoneApplicationFrame); 
    } 
} 

public static bool Navigate(Uri source) 
{ 
    if (CurrentRootVisual != null) 
     return CurrentRootVisual.Navigate(source); 

    return false; 
} 

public static void GoBack() 
{ 
    if (CurrentRootVisual != null) 
     CurrentRootVisual.GoBack(); 
} 

然後,你可以這樣做:

App.Navigate(yourNavigateUri) 

或 App.GoBack()

從任何地方你喜歡!

+0

這是一個很棒的技術,但唉!它不工作:(我仍然自己得到相同的錯誤。 – jedmao

+0

不,你不.. –

0
Dispatcher.BeginInvoke(() => 
    NavigationService.Navigate(new Uri("/home.xaml", UriKind.Relative))); 
相關問題