After successfully installing Xamarin as described in the first example, it's time to launch the first sample application.
In Visual Studio, choose New -> Project -> Visual C# -> Cross-Platform -> Blank App (Xamarin.Forms Portable)
Name the app "Hello World" and select the location to create the project and click OK. This will create a solution for you which contains three projects:
Having created the solution, a sample application will be ready to be deployed. Open the App.cs
located in the root of the portable project and investigate the code. As seen below, the Content
s of the sample is a StackLayout
which contains a Label
:
using Xamarin.Forms;
namespace Hello_World
{
public class App : Application
{
public App()
{
// The root page of your application
MainPage = new ContentPage
{
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Children = {
new Label {
HorizontalTextAlignment = TextAlignment.Center,
Text = "Welcome to Xamarin Forms!"
}
}
}
};
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
}
Now simply right-click the project you want to start (HelloWorld.Droid
or HelloWorld.iOS
) and click Set as StartUp Project
. Then, in the Visual Studio toolbar, click the Start
button (the green triangular button that resembles a Play button) to launch the application on the targeted simulator/emulator.