Reputation: 1432
I'm using Xamarin Forms for Android and IOs app.
I have two pages, Login page and Main page. The logic I want to implement is: Check if user is Logged In, If no open "Login" page, if yes go to "Main" page.
I was trying to implement this using NavigationPage and Navigation.PushAsync method. But method not working without NavigationPage, and NavigatetionPage renders additional navigation bar at the top.
Now I'm using:
App.xaml.cs
public App()
{
InitializeComponent();
if (!isUserLoggedIn)
{
MainPage = new LoginPage();
}
else
{
MainPage = new MainPage();
}
}
LoginPage.xaml.cs
//When user authenticated
App.Current.MainPage = new MainPage();
This code works well, but I'm not sure if this is good and correct solution.
Is there any better way to implement same logic?
Upvotes: 0
Views: 1714
Reputation: 15021
You just need hide the Navigationbar in your LoginPage and MainPage.
public App()
{
InitializeComponent();
if (!isUserLoggedIn)
{
MainPage = new NavigationPage(new LoginPage());
}
else
{
MainPage = new NavigationPage(MainPage());
}
}
LoginPage.xaml.cs:
public LoginPage()
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);
}
//When user authenticated
App.Current.MainPage = new NavigationPage(MainPage());
MainPage.xaml.cs:
public MainPage()
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);
}
Upvotes: 1