2011-02-17 16 views
3

我無法通過用戶控件調用導航服務。 即使我在主頁上創建一個事件處理程序來調用老撾不工作的導航服務。用戶控制在win 7手機中導航

你能幫我嗎?

+1

請顯示你正在使用的一些代碼,並解釋什麼是不工作 - 是一個異常被拋出?它是不是默默地導航? –

回答

7

我想我看到了這個問題,但是就像奧斯汀所說的那樣,在你的初始描述中沒有太多東西可以繼續。這聽起來像是您正嘗試從您在該頁面上放置的用戶控件中訪問NavgationService(這是一個PhoneApplicationPage屬性)。

與這些API中的許多事情一樣,您有幾個選項。首先,你可以訪問PhoneApplicationFrame(其中包含頁面和管理導航),並用它來導航:

var frame = App.Current.RootVisual as PhoneApplicationFrame; 
frame.Navigate(new Uri("/TargetPage.xaml", UriKind.Relative)); 

或者,您可以使用VisualTreeHelper走控件的視覺樹,直到到達包含頁面:

var page = GetParentOfType<PhoneApplicationPage>(this); // this is your user control 


private static T GetParentOfType<T>(DependencyObject item) where T : DependencyObject 
{ 
    if (item == null) throw new ArgumentNullException("item"); 
    T result; 
    var parent = VisualTreeHelper.GetParent(item); 
    if (parent == null) return null; 
    else if (parent.GetType().IsSubclassOf(typeof(T)) 
    { 
      result = (T)parent; 
    } 
    else result = GetParameterOfType<T>(parent); 
    return result; 
} 

正如你看到的,的VisualTree方法涉及更多的代碼,但是讓你的包含頁面對象,那就是你有喜歡的東西NavigationContext更多的訪問等

希望使得w作爲你的問題(和你的答案)。

+0

3歲,仍然這幫了我很多:)謝謝你! – ecth