Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you switch between two different Xamarin Form pages?

I'm trying to figure out how to switch between two different pages in Xamarin Forms.

I do not wish to use a NavigationPage (which has that little back arrow that is auto displayed.

I have a Login page (which is a ContentPage) and once the person has authenticated, I then need to navigate to my Dashboard page (which is a TabbedPage).

eg.

enter image description here

Next, one of the Tab's in the TabbedPage is the profile of the logged in user. As such, I need to log them out. So i'll have a button to log them out, which means I will need to navigate them back to the Login page (which was that ContentPage).

enter image description here

I feel like I have two modes the user might be in.

  • UnAuthorized. (ContentPage)
  • Authorized. (TabbedPage).

It's like I need to change the App's MainPage to be either one of those two?

like image 280
Pure.Krome Avatar asked Aug 05 '15 08:08

Pure.Krome


People also ask

How do I redirect to another page in Xamarin?

xaml. Go to Solution Explorer-->Your Project-->Portable-->Right click-->Add-->New Item (Ctrl+Shift+A). Now, select the Forms XAML page and give the name (SecondPage. xaml).


2 Answers

To change MainPage to another just do:

App.Current.MainPage = new NavigationPage(new MyContentPage());

or

App.Current.MainPage = new MyContentPage();

BTW: You can use a NavigationPage and then HIDE the toolbar with:

NavigationPage.SetHasNavigationBar(this, false);
like image 97
Daniel Luberda Avatar answered Oct 06 '22 01:10

Daniel Luberda


You can do navigation as usual by setting MainPage in your Application instance. Small sample.

namespace TestNavigation
{
    public class App : Application
    {
        public App ()
        {
            // The root page of your application
            MainPage = new MyPage1 ();
        }
    }
}


namespace TestNavigation
{
    public class MyPage1 : ContentPage
    {
        public MyPage1 ()
        {
            var button = new Button () {
                Text = "Click me"
            };
            button.Clicked += (object sender, EventArgs e) => {
                Application.Current.MainPage = new MyPage2 ();
            };
            Content = new StackLayout { 
                Children = {
                    button
                }
            };
        }
    }
}


namespace TestNavigation
{
    public class MyPage2 : ContentPage
    {
        public MyPage2 ()
        {
            Content = new StackLayout { 
                Children = {
                    new Label { Text = "Hello ContentPage" }
                }
            };
        }
    }
}

EDIT1

I submitted this answer but then I realised that I probably don't understand your problem.

EDIT2

Updated sample.

like image 32
Artur Shamsutdinov Avatar answered Oct 05 '22 23:10

Artur Shamsutdinov