Xamarin.Forms is a cross platform that allows us to easily create a user interface layout that can be shared across Android, iOS and Windows Phone. We can share most of the code across Mobile platforms. If our resources are limited and you are mostly going to focus on the functionality rather than the design, Xamarin.Forms is the best to develop a cross platform.
Let’s start.

We will try to create a sample Application in this session, how to create a login page, and how to navigate from one form to another. In the coming articles, we will focus on the menu and toolbar.
Step 1

File->New Project ->Templates ->Visual C# ->Cross platform ->Blank App(Xamarin.Froms.Portable).

Select blank app, the project name and project location.



Once the application is created, it will look as given below. EmployeeInfo is our shared project. We can place our code and design and this project is going to be shared by both Android and iOS.

Step 2

Before doing anything, I just created some folders for my convenience and now, we have to create a login page. For this, right click in View folder and Select Add-->New Item.

Select Cross-Platform under Visual C# --> Forms ContentPage, add a name and click OK. Likewise,create MainPage.



Step 3:
We can add a login form and the code is given below:

MainPage.cs
  1. public class MainPage : ContentPage
  2. {
  3. public MainPage()
  4. {
  5. Content = new StackLayout
  6. {
  7. VerticalOptions = LayoutOptions.Center,
  8. Children = {
  9. new Label {
  10. HorizontalTextAlignment = TextAlignment.Center,
  11. Text = "Welcome to EmployeeInfo Xamarin Forms!"
  12. }
  13. }
  14. };
  15. }
LoginPage.cs
  1. public class LoginPage : ContentPage
  2. {
  3. private readonly Entry _userName;
  4. private readonly Entry _password;
  5. public LoginPage()
  6. {
  7. var add = new Button
  8. {
  9. Text = "Login",
  10. TextColor = Color.White
  11. };
  12. _userName = new Entry { Placeholder = "UserName" };
  13. _password = new Entry { Placeholder = "Password", IsPassword = true };
  14. add.Clicked += Add_Clicked;
  15. var stackLayout = new StackLayout
  16. {
  17. Spacing = 20,
  18. Padding = 50,
  19. VerticalOptions = LayoutOptions.Center,
  20. Children =
  21. {
  22. _userName,
  23. _password ,
  24. add
  25. }
  26. };
  27. Content = stackLayout;
  28. }
  29. private void Add_Clicked(object sender, EventArgs e)
  30. {
  31. if (_userName.Text == "a" && _password.Text == "a")
  32. {
  33. Application.Current.MainPage = new MainPage();
  34. }
  35. else if (string.IsNullOrEmpty(_userName.Text) || string.IsNullOrEmpty(_password.Text))
  36. {
  37. DisplayAlert("Error", "Username and Password are required", "Re-try");
  38. }
  39. else
  40. {
  41. DisplayAlert("Failed", "Invalid User", "Login Again");
  42. }
  43. }
  44. }
App.cs
  1. public class App : Application
  2. {
  3. public App()
  4. {
  5. MainPage = new LoginPage();
  6. }
  7. protected override void OnStart()
  8. {
  9. // Handle when your app starts
  10. }
  11. protected override void OnSleep()
  12. {
  13. // Handle when your app sleeps
  14. }
  15. protected override void OnResume()
  16. {
  17. // Handle when your app resumes
  18. }
  19. }
Step 4: Run the Application.



Enter the username, password and click Login.



We created a sample EmployeeInfo Application and added Login page and added some code to navigate around the Application. In the coming articles, we will focus on the Menu and Toolbar.