2012-01-06 3 views
19

Làm thế nào để tôi có quyền truy cập NavigationService trong ứng dụng Windows Phone mà không phải trải qua PhoneApplicationPage? Mục tiêu của tôi là chuyển nó sang mô hình khung nhìn chính của ứng dụng khi khởi động, một kỹ thuật hoạt động khá tốt với tôi trong WPF và Silverlight.Làm thế nào để tôi có thể truy cập NavigationService trong ứng dụng Điện thoại Windows mà không phải trải qua PhoneApplicationPage?

Trả lời

35

Bạn có thể tải ứng dụng từ số PhoneApplicationFrame của ứng dụng. Nó sẽ có thể truy cập từ mọi nơi trong ứng dụng vì mọi ứng dụng Windows Phone đều có Khung.

((PhoneApplicationFrame)Application.Current.RootVisual).Navigate(...); 
+2

này đã cứu tôi khoảng 30 phút của tìm kiếm - tình yêu công việc của bạn! – Doug

+0

@Doug - Cảm ơn! Vui mừng được giúp đỡ bất cứ khi nào tôi có thể :) – keyboardP

+1

Cảm ơn rất nhiều bạn thân. Tiết kiệm thời gian của tôi quá. – NoobDeveloper

1

Một nơi khác để có được nó là từ lĩnh vực RootFrame trong việc thực hiện mặc định của ứng dụng:

#region Phone application initialization 

    // Avoid double-initialization 
    private bool phoneApplicationInitialized = false; 

    // Do not add any additional code to this method 
    private void InitializePhoneApplication() 
    { 
     if (phoneApplicationInitialized) 
      return; 

     // Create the frame but don't set it as RootVisual yet; this allows the splash 
     // screen to remain active until the application is ready to render. 
     RootFrame = new PhoneApplicationFrame(); 
     RootFrame.Navigated += CompleteInitializePhoneApplication; 

     // Handle navigation failures 
     RootFrame.NavigationFailed += RootFrame_NavigationFailed; 

     // Ensure we don't initialize again 
     phoneApplicationInitialized = true; 
    } 

    // Do not add any additional code to this method 
    private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e) 
    { 
     // Set the root visual to allow the application to render 
     if (RootVisual != RootFrame) 
      RootVisual = RootFrame; 

     // Remove this handler since it is no longer needed 
     RootFrame.Navigated -= CompleteInitializePhoneApplication; 
    } 


    #endregion