Introduction
In this article, you will learn how to create a new Xamarin cross-platform project. And also, you'll learn how to run it on different platforms. Follow these steps to build a cross-platform app.
- If you have Xamarin successfully installed, just open “Visual Studio” and click on “File” on the top left corner and go through “New” to click on “Project”.
- After you click, a new window will open. Click “Cross-Platform” if you want to build a cross-platform application. You can also select Android or iOS for the platform specific application. Now, choose “Mobile App (Xamarin Forms)” and name the project as you like; then click “OK”.
- There are multiple types of pages in Xamarin about which you will learn in my upcoming articles. You can make “Master Detail”, “Tabbed” or many other types of pages. But for now, as a beginner, simply select “Blank” and click “OK”. The project will start to create. It will take some time whenever you create a new project.
- After the project is created, you can run project specific to your OS and device. Select “UWP” for Windows and “Local Machine” on the top of the bar. For “Android”, you should have a smartphone or an emulator installed on your PC. Visual Studio 2017 provides some built-in emulators for Android so you don’t need to worry about them. So, in that case, select Android and an emulator on which you want to test the project. If you have an Android device connected with your PC, simply select that device. In the case of iOS, you should have an Apple device.
The first time the page consists of a ContentPage and a Label with a Text “Welcome to Xamarin.Forms!”
- It will take some time the first time you run the project so just keep calm. To reduce the time, you can simply go to “Build” in the menu bar and open “Configuration Manager” and select your Platform to build a project on. Unmark unnecessary options to save time. For now, I am going to select “UWP”.
- After that, click the Play button naming “Local Machine” and run the project. As I told you before, it will take some time the first time you run it. And this will be your first output.
XAML Code
- <?xml version="1.0" encoding="utf-8" ?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:PracApp1" x:Class="PracApp1.MainPage">
- <StackLayout VerticalOptions="Center" HorizontalOptions="Center">
- <Label Text="Welcome To Xamarin Forms!"></Label>
- </StackLayout>
- </ContentPage>
C# Code
- using PracApp1.Models;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Xamarin.Forms;
- namespace PracApp1 {
- publicpartialclassMainPage: ContentPage {
- public MainPage() {
- InitializeComponent();
- }
- }
- }
Creating applications with Xamarin is much easier compared to other languages because it allows you to build a cross-platform application. I hope reading this article allowed you to create new Xamarin project much more efficiently and provides you the knowledge about cross-platform and platform-specific applications.