【发布时间】:2012-01-06 09:09:19
【问题描述】:
如何在不通过 PhoneApplicationPage 的情况下在 Windows Phone 应用程序中访问 NavigationService?我的目标是在启动时将其传递给应用程序的主视图模型,这种技术在 WPF 和 Silverlight 中对我来说非常有效。
【问题讨论】:
标签: windows-phone-7
如何在不通过 PhoneApplicationPage 的情况下在 Windows Phone 应用程序中访问 NavigationService?我的目标是在启动时将其传递给应用程序的主视图模型,这种技术在 WPF 和 Silverlight 中对我来说非常有效。
【问题讨论】:
标签: windows-phone-7
您可以从应用程序的PhoneApplicationFrame 获取它。由于每个 Windows Phone 应用程序都有一个框架,因此可以从应用程序的任何位置访问它。
((PhoneApplicationFrame)Application.Current.RootVisual).Navigate(...);
【讨论】:
另一个获取它的地方是从Application的默认实现中的RootFrame字段:
#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
【讨论】: