2

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?

user2799350
  • 389
  • 1
  • 4
  • 14

3 Answers3

4

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=""/>
nirmus
  • 4,913
  • 9
  • 31
  • 50
  • hi @nirmus. there is an error in `((App)Application.Current).Root..`. it says `MyApplication.App.RootFrame.Get` cannot be accessed with an instance reference. qualify it with a type name instead – user2799350 Oct 03 '13 at 21:57
  • it has to work. Post your code, maybe you did some other error – nirmus Oct 03 '13 at 21:59
  • I just copy/pasted your code to application launching. when I write `((App)Application.Current).` the `RootFrame` doesn't appear in intellisense. – user2799350 Oct 03 '13 at 22:03
  • Application.Current is public singleton: http://msdn.microsoft.com/en-us/library/system.windows.application.current.aspx. It must exist;) – nirmus Oct 03 '13 at 22:06
  • @nirmus I don't think your way will work because the navigation will overlap with the navigation to the default page defined in WMAppManifest and if it did work then it will navigate to the original page first than the second page which will create a flashing effect – Benoit Catherinet Oct 03 '13 at 22:13
  • 1
    @Benoit Catherinet - check my edited post. I mention about clear NavigationPage in WMAppManifest – nirmus Oct 03 '13 at 22:16
  • @nirmus Actually I was curious if you were allowed to clear the NavigationPage and tried it and it seem to work but the WMAppManifest complain saying that the NavigationPage parameter is required so I wonder if it will pass store certification ... – Benoit Catherinet Oct 03 '13 at 22:37
  • I didn't remove NavigationPage parameter. I've just cleared it. – nirmus Oct 03 '13 at 22:46
  • The exact error message of the AppManifest designer is "Default Navigation Page : is a required field and cannot be blank" – Benoit Catherinet Oct 03 '13 at 23:02
3

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.

Benoit Catherinet
  • 3,335
  • 1
  • 13
  • 12
0

Changing the MainPage at startup is when a new Instance of the App class gets initiated.... in the App.xaml.cs page the Application should first initiate a new AppShell component from within the ctor. If it doesn't then the Shell.Current Object will always be null; However, you may use the OnStart method since this is available to us and in there then navigate to the correct Login Page or whatever...

    public App()
    {
        InitializeComponent();
        XF.Material.Forms.Material.Init(this);
        DependencyService.Register<MockDataStore>();
        //MainPage = new AppShell();
        MainPage = new AppShell();
        //GoToLoginPageOnStart();
    }


    protected override void OnStart()
    {
        // Handle when your app starts
        Shell.Current.GoToAsync("//LoginPage");
       

    }
bilaalsblog
  • 159
  • 1
  • 12