Xamarin.Forms

I have started using Xamarin.Forms, a wrapper for MonoTouch (a .NET bridge to Cocoa Touch), MonoDroid (a .NET bridge to Android) and for the "Windows Runtime", Microsoft's API for Windows Mobile and other Windows-based platforms.

Xamarin.Forms is now a Microsoft product and part of recent updates of Visual Studio.

Xamarin.Forms can be used in Visual Studio (and then requires a Mac to create the iOS binary) or in Xamarin Studio, the version of MonoDevelop with the Xamarin software stack (which does not support the Windows Runtime target).

A Xamarin.Forms solution consists of one shared or Portable Class Library project and one or more target projects, usually targeting iOS, Android and the Universal Windows Platform, an, if needed, also Windows 8 and Windows Phone 8.

A Portable Class Library is a .NET DLL that can be used by .NET programs on any .NET-supported platforms. The alternative here is a shared project, which is compiled into the target binaries.

Xamarin Studio:

xamarinstudio

MonoDevelop (Xamarin Studio without Xamarin):

monodevelop

Visual Studio:

visualstudio


In Visual Studio a Xamarin.Forms solution looks like this:

uwpproject

The highlighted project here is the "Universal Windows Platform" project for the Windows and Windows Mobile target. Other projects shown here are "Hello (Portable)", the Portable Class Library and shared component of the target projects, "Hello.Droid", the Android target project, and "Hello.iOS", the iOS target project. Missing are "Hello.Windows" and "Hello.WinPhone" which would be target projects for Windows 8 and Windows Phone 8 respectively. (I think one can add other target projects that also use the solution's Portable Class Library project but not the Xamarin.Forms parts of it.)

The compiled (but otherwise unchanged from the template) projects look like this:

Android (on the Xamarin Android emulator running Android 4, I think):

android

iOS (on Apple's iOS simulator running iOS 9.2):

iphone

Windows:

uwp1

The Windows program runs on both Windows 10 and Windows 10 Mobile. It runs in fullscreen on Windows 10 Mobile and Tablet Mode and in its own window in Desktop Mode.


I ran into some problems creating and compiling the solution.

While the iOS and Android projects worked fine, the Universal Windows Platform project would not compile.

When building the project, Visual Studio thought the following error message would be appropriate:

Type universe cannot resolve assembly: System.Runtime

I could fix this, for some reason, by copying a "project.lock.json" file from another Universal Windows Platform project. This somehow added a few DLLs and resolved this issue and made the project happily run into the next issue, which Visual Studio happily announced as:

The type or namespace name 'Xamarin' could not be found in the global namespace

You can see why that would be a problem, given that this is a Xamarin project! I could see that under References there were no entries for the Xamarin DLLs. I found those in the solution's "packages" folder and added them manually to the Universal Windows Platform project. The needed DLLs were these:

  • Xamarin.Forms.Core
  • Xamarin.Forms.Platform
  • Xamarin.Forms.Platform.UAP
  • Xamarin.Forms.Xaml

Finally, special precautions have to be taken when adding the project to source control. Source control clients do not upload all files (for example not object code created by the build process or temporary files) but only source files and (hopefully) all resources. But for a Xamarin.Forms project the "packages" folder of the solution has to be added as well and so do the "packages.config" files in the Android and iOS projects. (I assume the project.lock.json" also should be added.)

Also note, and this appears to be quite unrelated to Xamarin.Forms that Visual Studio error -1988945902 (which incidentally simply cannot be found with Google) translates to 0x89731812 and essentially means "cannot find your Windows 10 Mobile device". Disconnect and reconnect the phone to fix this.

Update

Also a good tip: If Visual Studio suddenly announces that

'Resource.Color' does not contain a definition for 'abc_color_highlight_material'

it means you have to change the Android API version of the Droid target project. Make it Android 6.0.

 © Andrew Brehm 2016