Join Mark Smith for an in-depth discussion in this video Exercise 1: Creating your first Xamarin.Forms app, part of Introduction to Xamarin.Forms.
- [Instructor] Now we're going to go ahead and create our first Xamarin.Forms application, so let's go up to File, let's go to New Project, and we're going to go to Cross Platform, and we're going to create a cross platform app Xamarin.Forms or Native. I'm going to call this First App, and I'm going to click OK. And when I do that, I'm going to create a blank application using Xamarin.Forms, and you can choose from a shared project or a portable class library.
I'm going to choose portable class library for now, and just click OK. This will go off and now create the project, and this may take a few minutes because it's creating projects for each of the platforms. All right, so our Xamarin.Forms solution has been loaded, and I want to point out a couple of things, and I want to run it on iOS and Android just so you can get a feeling as to how this all works. So the first thing is our portable class library. Remember this is our shared location, and this is where most of our code will go.
Now this is the home right now of our UI, so you can see we have an App.xml, a MainPage.xml, and we're currently inside the App.xml.cs. Now the application class is actually responsible for a lot of things, but you can see a couple of things going on here. First we have this MainPage assignment. This MainPage property is what is basically the startup page. So when we launch this to Xamarin.Forms application, the main page will be displayed.
And then you can also see some life cycle methods, so if you want to put some code in here this code will run when the application first starts. This code in OnSleep will run when the application goes into the background, and so on. So if you look in the solution explorer, you can also see we have an Android iOS NUWP project. So if I go into the Android project and go into Main Activity.cs, notice how there's some code that's specific to Xamarin.Forms here.
First notice that the activity now derives from a Xamarin.Forms activity, and then also we have this Xamarin.Forms init-- This initializing the Xamarin.Forms framework, and you can also have this call to LoadApplication new app. This new app right here, that is the Application.xml, that's this file that we just saw a second ago. If I go inside of the iOS project and I go inside the app delegate, you can see a very similar idea.
AppDelegate is deriving from FormsApplicationDelegate, and inside of FinishedLaunching I'm initializing the Xamarin.Forms framework and loading the application. So what I want to do is I want to go ahead and run this on Android and iOS just to show you what's going on. Let me just go ahead and open up MainPage.xml first, just to show you what our UI looks like. All right, so here you can see the MainPage.xml consists of a ContentPage, and inside the ContentPage we have a single label that says welcome to Xamarin.Forms.
So let's go ahead and get started by writing this on Android and seeing what it looks like on Android. All right, so here we are, the Android version is loaded, and you can see the label that says, Welcome to Xamarin.Forms! So that seems to be working fine. Let's go ahead now and let's try to run this on iOS. So to do that I'm going to switch my startup project to the FirstApp.ios version. I could have also done that by right-clicking on the project and going to Set As Startup Project.
So for example, I could have clicked on that. And now let's go ahead and we're going to try to run that in iPhone Simulator, but you can use the iPhone 7, that's fine. I'll go ahead and hit Play, and let's see what happens here. All right, and here is our application running on iOS. Once again you can see the same welcome to Xamarin.Forms message. Now on a side note, if you're wondering how I'm presenting this iPhone on my Windows computer, I'm using the Xamarin remoting feature for iOS.
This is actually a Visual Studio Enterprise feature, and basically it's just presenting my iOS simulator from my Mac onto my Windows computer.
This course was created by Xamarin University. We are honored to host this training in our library.