Your First Xamarin.Forms Project
While Xamarin is an attractive option for creating mobile applications in Visual Studio, it doesn't mean that getting started is easy. For example, while you can target iOS devices with Xamarin, you'll need a Macintosh computer on your network to handle compiling your application. I'm going to avoid that issue in this article, but I'll cover everything else you need to know.
Creating Your Project
Before starting your Xamarin project, I recommend installing some version of Visual Studio 2017 -- it has the best support for Xamarin. For this article I'm using Visual Studio 2017 Enterprise Edition (thanks to the MVP Reconnect program -- I'm too cheap to pay for it). However, Visual Studio 2017 Community Edition is free and has everything you need.
To start your project in Visual Studio, from the File menu select New Project and, under the C# | Cross-Platform tab, select Mobile App (Xamarin.Forms). After giving your project a name (I used "HelloWorld") and clicking the OK button, you'll be presented with a Wizard that lets you pick:
- An initial template (I used the Blank App template)
- One or more platforms (I left the Android option checked but unchecked both iOS and Windows)
- A code sharing strategy (I picked .NET Standard)
The alternative in Visual Studio 2017 to .NET Standard Project is Shared Project (in earlier versions it's a Portable Class Library or PCL Project). Standard Project is a better choice because Shared Projects provide no IntelliSense support for using XAML to define your Xamarin.Forms (and XAML is a great way to design your Xamarin.Forms).
After you've made your choices, clicking the OK button creates your Visual Studio Solution.
Configuring Your Android Project
Because I only targeted Android devices, my solution only has two projects in it: A standard project named HelloWorld and a project targeted for the Android platform named HelloWorld.Android. Had I picked iOS or Windows, I would have ended up with additional projects targeted for each of those platforms.
Your Android project gets its Xamarin support through a set of NuGet Packages. With NuGet Packages it's usually a good idea to make sure that you have the latest versions. However, if you try to update the Xamarin packages that are flagged in NuGet Manager as having updates, you may find that you can't install the packages.
The problem is that, to use the latest versions of the Android NuGet Packages, your project has to target the latest version of the Android OS. To set your project to use the latest version of the Android OS, open your Android project's properties, select the Application* tab and, from the Compile Using Android Version dropdown list, select the latest version of the Android OS (I picked Oreo).
But you may not want to move to a more recent version of the Android OS. The more recent the version of Android that you target, the fewer devices that your application is guaranteed to run on (not that your application won't run on every Android device – it's just not guaranteed to run). Rather than changing your project type, you can try different versions of each of the various NuGet packages that are marked for update until, by trial and error, you find the latest version that you can use of each.
You're also going to need install the appropriate Android SDK. With Visual Studio 2017 you can use the Android SDK Manager (available from Visual Studio's Tools > Android menu) to download and install the SDK. If you're not using Visual Studio 2017, you can get the SDK with Android Studio and then run the SDK manager from your Windows Start menu.
Once the SDK is installed, in the SDK manager's Tools tab, check to see if any items are flagged with "Update available." If you find any click the n Updates Available button in the lower left-hand corner (n will be the number of updates available). On the following dialog, click the Install Updates button and, on the next dialog, the button that says that you accept the relevant license. While I was on the Tools tab, I also chose to install all the versions of the Android Build Tools by checking them off and clicking the Apply Changes button.
You've now got a Xamarin project that's ready to accept code and run. I'll walk through creating and debugging a Hello, World project in an upcoming column.
About the Author
Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.