如何在不通过PhoneApplicationPage的情况下访问WIndows Phone应用程序中的NavigationService?

时间:2012-01-06 00:10:53

标签: windows-phone-7

如何在不通过PhoneApplicationPage的情况下访问Windows Phone应用中的NavigationService?我的目标是在启动时将它传递给应用程序的主要视图模型,这种技术在WPF和Silverlight中对我很有用。

2 个答案:

答案 0 :(得分:35)

您可以从应用的PhoneApplicationFrame获取。它可以从应用程序的任何位置访问,因为每个Windows Phone应用程序都有一个Frame。

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

答案 1 :(得分:1)

另一个获得它的地方是来自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