Introduction to Xamarin
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:
- Android
- iOS
- Windows (UWP)
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#.
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:
- A .NET standard library project.
- An Android project.
- An iOS project.
- 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 examples (Overview).
- Weather Rest API.
Xamarin.Forms:
- Xamarin.Forms examples (Overview).
- …
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
. 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
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:
- Build solution will perform an incremental build: if it doesn't think it needs to rebuild a project, it won't. It may also use partially-built bits of the project if they haven't changed (I don't know how far it takes this)
- Rebuild solution will clean and then build the solution from scratch, ignoring anything it's done before. The difference between this and “Clean, followed by Build” is that Rebuild will clean-then-build each project, one at a time, rather than cleaning all and then building all.
- Clean solution will remove the build artifacts from the previous build. If there are any other files in the build target directories (bin and obj) they may not be removed, but actual build artifacts are. I've seen behaviour for this vary - sometimes deleting fairly thoroughly and sometimes not - but I'll give VS the benefit of the doubt for the moment :)
The links are to the devenv.exe command line switches, but they do the same as the menu items.)
The
“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:
- The
.git
sub-folder which contains the Git repository (only if the solution uses a CCS). - Th
.vs
sub-folder. It used by VS for various housekeeping activities. - The
obj
andbin
sub-folders that are created in each project's folder. These are created at build time. - 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
(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
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.
Library cannot be started directly
When you try to debug a project, you get:
Remedy: Go through these steps:
- Right click the project name you want to debug in the Solution Explorer (e.g. Myproject.Android).
- Select “Set as StartUp Project” from the menu.
- 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
.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].