Introduction to Xamarin

by Gisle Hannemyr

This chapter is an introduction to Xamarin. It tells you how to locate information about Xamarin. It also introduces the basic components of Xamarin.

Table of contents

Introduction

Xamarin is a component for Microsoft's Visual Studio maintained by a Microsoft-owned component with the same name. For more, see Wikipedia.

Of particular interest is the cross-platform Xamarin.Forms. It provides a C#-shared codebase, leveraging of Microsoft .NET, for the backend business logic, and converts shared C# to produce a native user interface for several mobile platforms. Using Xamarin.Forms, developers can use Xamarin tools to write native apps for:

To develop for Apple iOS on MS Windows, you need to pair the MS Windows PC with an Apple Mac.

Using Xamarin.Forms, the same programming language, APIs and data structures can be shared cross all mobile development platforms. Xamarin.Forms even produces native code for the user interface, allowing most of it to be shared, with only a tiny portion of it requiring platform-specific C#.

xamarin01.png
Xamarin lets all of the logic and most of the user interface to be shared C#,
with only a tiny portion of the user interface being platform-specific C#.

In addition to Xamarin.Forms there is Xamarin.Native (aka. “Traditional Xamarin”). This variant of Xamarin provides a shared backend, but requires the user interface to be native. For more about Xamarin.Forms vs. Xamarin.Native, see this answer on StackOverflow.com.

Terminology

Xamarin uses the word “solution” to designate a bundle of projects, where each “project” is targeted for a specific platforn. For instance, a cross-platform solution based upon the .NET standard library for code sharing may consist of up to four projects:

  1. A .NET standard library project.
  2. An Android project.
  3. An iOS project.
  4. A Windows (UWP) project.

Finding information about Xamarin

There exists many sources that provides documentation, tutorials and other information about Xamarin, including some well-written books. However, the most up to date information is on the web. The following links are the some starting points when looking for information about Xamarin on the web:

Xamarin:

Xamarin.Forms:

Installing Xamarin

Before you install Xamarin, you should make sure you have a working version of Android Studio, with the ability to debug on a physical device. For instructions, please see Installing Android Studio.

Microsoft has provided detailed instructions about installing Xamarin. However, just downloading the installer and running it seems to work.

When the installation is complete, you should check for any Android SDK Updates. In Visual Studio, navigate to Tools » Android » Android SDK Manager. The column with the heading “Status” will tell you about any available updates. If there are any updates, select and install them.

At this point, you will be able to build Android and iOS applications using Visual Studio. To verify that the install was successful you can create a new Android or iOS minimal project and test it. The project should build and run without any issues. See the section about troubleshooting below for remedies for any issues.

Basic commands

To open a solution, make sure the solutions .zip-file is unpacked to regular directory. Navigate to the solution root and click on the .sln-file.

To run a project to debug it Debug » Start debugging or click the Debug button.

To delete a solution, just delete it in the file system.

To delete a project, select it in the Solution Explorer, then hit Delete.

To rename a project, select it in the Solution Explorer, right-click and select “Rename”.

The build menu

The build menu provides these options:

The links are to the devenv.exe command line switches, but they do the same as the menu items.)

noteThe “Clean Solution” is reported to be unreliable (i.e. not cleaning up everything, causing phantom errors). The recommended practice is to clean up a project is to delete the bin and obj folders in the project's home directory.

Source: StackOverflow.

Saving a solution

Visual Studio have no built-in command to save a solution into something than can be exported to another machine or shared with other developers.

However, you may delete the follwing:

  1. The .git sub-folder which contains the Git repository (only if the solution uses a CCS).
  2. Th .vs sub-folder. It used by VS for various housekeeping activities.
  3. The obj and bin sub-folders that are created in each project's folder. These are created at build time.
  4. Any .user-files. These are used by VS for local user settings.

Copy the solution, delete folders and files listed above from the copy. Pack whar remains in a .zip-file.

Source: StackOverflow.

Troubleshooting

Getting started using Xamarin can be a frustrating experience. Here are the hurdles I encountered, and how they were remedied.

Built-in android emulator not working

Xamarin ships with a built-in android emulator that you can inspect by navigating to Tools » Android » Android device manager (requires administrator access). By default this is a device with an Intel “x86” processor.

There is problem with this emulator if running a project in debug mode results in:

There were deployment errors. Continue?

And clicking “Yes” produces the following error message:

Object reference not set to an instance of an object.

The Android emulator in Android Studio requires Hyper-V to be enabled to create virtual machines. This software is not available on Windows 10 Home, only exists on Windows 10 Enterprise, Pro, or Education.

You can verify that there is a problem with the emulator by navigating to Tools » Android » Android device manager and clicking “Start”. If the emulator is unusable, this produces the following error message.:

Could not start emulator
Could not launch the emulator.
Please file an issue by choosing "Report a Problem" in the Help menu, and attach your logs. Error message: "Unable to load DLL 'DismApi': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"

Remedy: Connect a physical Android device using USB.

xamarin04
Location of “Debug”-button and were to see whether a physical Android device us connected.

Library cannot be started directly

When you try to debug a project, you get:

xamarin08.png
A project with an Output Type of Class Library cannot be started directly.

Remedy: Go through these steps:

  1. Right click the project name you want to debug in the Solution Explorer (e.g. Myproject.Android).
  2. Select “Set as StartUp Project” from the menu.
  3. Re-run your project.

Source: Stackoverflow.com.

Too old .NET SDK

The current .NET SDK does not support targeting .NET Core 2.1

Remedy: Download and install the most recent version from Download .NET SDKs for Visual Studio.

Build incompatible with .NET SDK

After upgrading the .NET SDK, you may run into this error:

Your project does not reference "…" framework. Add a reference to "…" in the "TargetFrameworks" property of your project file and then re-run NuGet restore.

This is caused by a file named project.assets.json in a folder named obj in the project folder (navigate filesystem, not solution), This file may point at the previous .NET Framework.

Remedy: Delete this file, the restart Visual studio and do a Build » Clean Solution.

Sources: developercommunity.visualstudio.com, StackOverflow.com

Build warnings

These build warnings are annoying, but does not stop project from building. I do not know about a working solution.

Found conflicts between different versions of "Microsoft.CSharp" that could not be resolved. These reference conflicts are listed in the build log when log verbosity is set to detailed.

Probably problem with NuGet having wrong version of Newtonsoft.Json.

No way to resolve conflict between "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" and "mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e". Choosing "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089".

Will always use most recent version of library.

Final word


Last update: 2019-04-07 [gh].