How to change start page at startup?

狂风中的少年 提交于 2020-01-11 06:41:48

问题


my application, currently, goes to the MainPage.xaml at startup (I don't know where it has configured though).

I want to be able to start with another page in some conditions. I think I can add this code to Application_Launching() in App.xaml.cs page:

NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative));

but NavigationService is not available in App.xaml.cs.

How can I start the application with another page if foo == true?


回答1:


Changing start page in App.Xaml.cs:

private void Application_Launching(object sender, LaunchingEventArgs e)
{

        Uri nUri = new Uri("/SecondPage.xaml", UriKind.Relative);
        ((App)Application.Current).RootFrame.Navigate(nUri);

}

Setting static startup page in Property\WMAppManifest.xml file

<DefaultTask  Name ="_default" NavigationPage="SecondPage.xaml"/>

edit

Try it:

    private void Application_Launching(object sender, LaunchingEventArgs e)
    {
        Uri nUri = new Uri("/GamePage.xaml", UriKind.Relative);
        RootFrame.Navigate(nUri);
    }

and in Property\WMAppManifest.xml clear NavigationPage:

<DefaultTask  Name ="_default" NavigationPage=""/>



回答2:


Here is a way to navigate depending on a condition:

In the constructor of App.xaml.cs add:

RootFrame.Navigating+= RootFrameOnNavigating;

and then define RootFrameOnNavigating like this:

    private bool firstNavigation = true;
    private void RootFrameOnNavigating(object sender, NavigatingCancelEventArgs navigatingCancelEventArgs)
    {

        //by defaullt stringOfPageNameSetInWMAppManifest is /MainPage.xaml
        if (firstNavigation && navigatingCancelEventArgs.Uri.ToString().Contains(stringOfPageNameSetInWMAppManifest))
        {
            if (foo == true)
            {
                //Cancel navigation to stringOfPageNameSetInWMAppManifest
                navigatingCancelEventArgs.Cancel = true;

                //Use dispatcher to do the navigation after the current navigation has been canceled
                RootFrame.Dispatcher.BeginInvoke(() =>
                {

                    RootFrame.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
                });
            }
        firstNavigation = false;
    }

Another way will be to use a UriMapper to redefine what uri is navigated to when you navigate to a certain page.



来源:https://stackoverflow.com/questions/19169657/how-to-change-start-page-at-startup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!