Introduction
This article demonstrates how to manipulate the navigation stack panel in order to only display the main page of the application once the user has successfully logged in.
Let's start.
Step 1
Open Visual Studio 2017. Select Templates >> Visual C# >> CrossPlatform.
Open Visual Studio 2017. Select Templates >> Visual C# >> CrossPlatform.
Select CrossPlatform App(Xamarin.Forms or Native) and give the application a suitable name, such as Login_Flow.
Now, choose project location and click OK.
Step 2
Now, create an empty XAML page named SignUpPage.xaml. For that, go to Solution Explorer >> Login_Flow(PCL) >> right-click and select a new item.
Now, create an empty XAML page named SignUpPage.xaml. For that, go to Solution Explorer >> Login_Flow(PCL) >> right-click and select a new item.
In the popup window, select Cross platform>>Empty XAML page. Give the page name as SignupPage.xaml. This page is your application signup page.
Double-click to get its design View and insert the code given below.
- <?xml version="1.0" encoding="utf-8" ?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
- xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
- x:Class="Login_Flow.SignupPage">
- <ContentPage.ToolbarItems>
- <ToolbarItem Text="LogIn" Clicked="LoginButtonClicked"/>
- </ContentPage.ToolbarItems>
- <ContentPage.Content>
- <StackLayout>
- <Label Text="Username"/>
- <Entry x:Name="UsernameEntry" Placeholder="Username"/>
- <Label Text="Password"/>
- <Entry x:Name="PasswordEntry" Placeholder="Password"/>
- <Label Text=" Email"/>
- <Entry x:Name="EmailEntry" Placeholder="Email ID"/>
- <Button Text="SignUp" Clicked="SignUpButtonClicked"/>
- <Label x:Name="errortext"/>
- </StackLayout>
- </ContentPage.Content>
- </ContentPage>
Step 3
In this step, open Solution Explorer>>Login_Flow(PCL)>>SignupPage.xaml.cs page and double-click to open design view. Here, the code is given below.
In this step, open Solution Explorer>>Login_Flow(PCL)>>SignupPage.xaml.cs page and double-click to open design view. Here, the code is given below.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- namespace Login_Flow
- {
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class SignupPage : ContentPage
- {
- public SignupPage()
- {
- InitializeComponent();
- }
- private async void LoginButtonClicked(object sender, EventArgs e)
- {
- await Navigation.PushAsync(new LoginPage());
- }
- async void SignUpButtonClicked(object sender, EventArgs e)
- {
- var user = new User
- {
- Username = UsernameEntry.Text,
- Password = PasswordEntry.Text,
- Email = EmailEntry.Text
- };
- var signUpSucceeded = AreDetailsValid(user);
- if(signUpSucceeded)
- {
- var rootpage = Navigation.NavigationStack.FirstOrDefault();
- if (rootpage != null)
- {
- App.IsUserLoggedIn = true;
- Navigation.InsertPageBefore(new MainPage(), Navigation.NavigationStack.First());
- await Navigation.PopToRootAsync();
- }
- else
- {
- errortext.Text = "SignUpError";
- }
- }
- }
- bool AreDetailsValid(User user)
- {
- return (!string.IsNullOrWhiteSpace(user.Username) && !string.IsNullOrWhiteSpace(user.Password) && !string.IsNullOrWhiteSpace(user.Email) && user.Email.Contains("@"));
- }
- }
- }
Step 4
Similarly, create an empty XAML page LoginPage.Xaml. This page is used for validating the user id and password. Here is the code for this page.This page is your application startup page.
Similarly, create an empty XAML page LoginPage.Xaml. This page is used for validating the user id and password. Here is the code for this page.This page is your application startup page.
- <?xml version="1.0" encoding="utf-8" ?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
- xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
- x:Class="Login_Flow.LoginPage"
- Title="LoginPage">
- <ContentPage.ToolbarItems>
- <ToolbarItem Text="SignUp" Clicked="SigUpButtonClicked"/>
- </ContentPage.ToolbarItems>
- <ContentPage.Content>
- <StackLayout>
- <Label Text="User Name or Email ID"/>
- <Entry x:Name="UserNameEntry" Placeholder="UserName"/>
- <Label Text="Password"/>
- <Entry x:Name="PasswordEntry" IsPassword="True"/>
- <Button Text="Click to Login" Clicked="Login_Clicked"/>
- <Label x:Name="LoginFaild"/>
- </StackLayout>
- </ContentPage.Content>
- </ContentPage>
Step 5
In this step, go to Solution Explorer>>Login_Flow(PCL)>>LoginPage.xaml.cs page.
In this step, go to Solution Explorer>>Login_Flow(PCL)>>LoginPage.xaml.cs page.



Ahsan SiddiquePosted Nov 13, 2017, 12:45 AM
Articles no need of just steps and code, article needs good explanation of code, than everyone can learn code logic's and concepts.
Ahsan SiddiquePosted Nov 13, 2017, 12:30 AM
Very poor explanation..