如何以编程方式在windows phone 8 app中设置启动页面?

时间:2014-03-05 08:36:58

标签: c# windows-phone-8 windows-phone

我想在查看配置中的一些数据后,以编程方式在我的Windows Phone 8应用程序中设置一个起始页面。我可以用哪个事件来做同样的事情?

提前完成。

3 个答案:

答案 0 :(得分:6)

我使用Application_Launching执行相同的任务。像这样:

    private void Application_Launching(object sender, LaunchingEventArgs e)
    {
        RootFrame.Navigated += RootFrame_Navigated;
        var logined = Singleton.Instance.User.Load();
        var navigatingUri = logined ? "/View/PageMainPanorama.xaml" : "/View/Account/PageLoginRegister.xaml";
        ((App)Current).RootFrame.Navigate(new Uri(navigatingUri, UriKind.Relative));
    }

答案 1 :(得分:2)

第一步是删除默认情况下我们在标准模板创建的应用程序中的清单文件(WMAppManifest.xml)中设置的默认页面。

只需从下面的代码中删除NavigationPage =“MainPage.xaml”。

<Tasks>
      <DefaultTask Name="_default" NavigationPage="MainPage.xaml" />
</Tasks>

通过调用RootFrame.Navigate()(在App.xaml.cs中)在InitializePhoneApplication()中指定起始页。。如下例所示

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;

            // Handle reset requests for clearing the backstack
            RootFrame.Navigated += CheckForResetNavigation;

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

            Uri uri;
            if (IsolatedStorageSettings.ApplicationSettings.Contains("islogin"))
            {
                if (!(Convert.ToString(IsolatedStorageSettings.ApplicationSettings["islogin"]).ToLower() == "yes"))
                {
                    RootFrame.Navigate(new Uri("/LoginScreen.xaml", UriKind.RelativeOrAbsolute));
                }
                else
                {
                    RootFrame.Navigate(new Uri("/HomeScreen.xaml", UriKind.RelativeOrAbsolute));               
                }
            }
            else
            {
                RootFrame.Navigate(new Uri("/LoginScreen.xaml", UriKind.RelativeOrAbsolute));
            }          
        }

答案 2 :(得分:1)

在这里,您可以找到描述起始页的动态设置的博客条目。

http://blogs.msdn.com/b/ptorr/archive/2010/08/28/redirecting-an-initial-navigation.aspx?wa=wsignin1.0