.net, Troubleshooting, Xamarin

Troubleshooting the default install of Xamarin with Visual Studio 2017 and Windows 10 Creators Update

Since I’ve recently started using Xamarin for cross platform development, I thought it would be nice to share with the community how I’ve got past some of the issues which tripped me up for a while when I was becoming familiar with it. This isn’t really a normal “getting started with Xamarin” tutorial – there’s lots of them already out there already, like this one – but hopefully anyone starting off with Xamarin will find it useful.

I use Windows 10 (and have installed the Creator’s Update) – this allows me to use Xamarin.Forms. You’ll know if you’re on the Creator’s Update version if your Windows build number is 1703. If you’re on a different version of Windows, you might have different experiences to me (you can check your version by going to Windows Settings -> System -> About).

Before we begin – what’s Xamarin and why should I use it?

With Xamarin tools built into Visual Studio, developers can create native applications in C# for Windows, Mac and Linux devices. So instead of writing and managing three different codebases for three different platforms, developers can just write their code once and deploy it to different app-stores.

Installing Xamarin tools for Visual Studio 2017

With Visual Studio 2017, it’s very easy just open up the setup wizard, select the Xamarin tools (as shown below) and just wait for it to install.

vs_community_image

It’s probably going to take a long time to install VS2017 with Xamarin – adding Xamarin to the base Visual Studio install makes it about 25GB bigger.

Tip: If you leave your machine to download and install Xamarin, it’s worth adjusting your power settings to make sure an unattended machine doesn’t switch off in the middle of the download – like mine did the first time (facepalm).

Creating a project with the default Xamarin template

This bit is straightforward to anyone who’s created a new project in Visual Studio 2017 before.

Select File -> New Project to open the dialog below, and choose a name for the project:

screenshot.1

After clicking OK on the dialog above (which chooses a Cross Platform App project type), the dialog will close and open a new project. I chose to use Xamarin.Forms (which allows developers to create cross platform user interfaces). I also chose to create a Shared Project because I only expect my code to be used in my application, rather than shared with other developers as a Portable Class Library (you can read more about the differences between Shared Projects and Portable Class Libraries here).

screenshot.2

When you click OK, the project and files will be created, and a window like the one below will appear with instructions for setting up the Mac Agent. (I don’t have a Mac and I’d need Visual Studio Enterprise to use this anyway, so I normally click on the “Don’t show this again” box in the bottom left corner).

screenshot.3

Finally you’ll be prompted for the versions of Windows that you want the UWP flavour of your project to target. I normally just click OK here.

screenshot.4

At this point, you’ll have a simple Xamarin solution in Visual Studio 2017, which contains 4 projects – one for iOS, one for Android, one for UWP, and one shared project.

Also notice that there is one file open in VS2017 after you create the solution – App.xaml.cs in the shared project. I’ll explain why this is relevant later.

And now for the gremlins ex machina

After this point, I hit a few snags. Things I wanted to do that didn’t work out of the box for me were:

  • Compile the application without error or warnings
  • Run the application in a Windows Phone Emulator
  • Run the application in an Android Emulator

I’ll run through some of the symptoms of problems I encountered trying the things above, and how I fixed them.

Compiling the solution led to multiple warnings and errors

Tip: Prepare to wait a while when building the solution for the first time – it needs to download a lot of NuGet packages.

Unfortunately my attempt to compile the project out of the box showed an error in the UWP project and a bunch of warning messages for the Android project.

Getting rid of the error CS0103 –  ‘InitializeComponent’ does not exist in the current context

The error reports “The name ‘InitializeComponent’ does not exist in the current context.”

screenshot-7

I eventually noticed a couple of things that seemed a bit bizarre:

  • Even though I have an error, the message in the status bar in the bottom left reports “Rebuild All succeeded” – both can’t be right surely?
  • This error relates to the App.xaml.cs file which is open in the editor panel. When I opened Main.xaml.cs from the Shared Project in the VS2017 editor, I now see two errors (as shown in the image below).

screenshot.13

So these errors don’t seem to negatively affect the build, and if I really want to get rid of them, I can just close those files which gets rid of the errors (as shown below).

screenshot.11

Getting rid of warnings about $(TargetFrameworkVersion) mismatches

Three of the warnings I saw were very similar:

The $(TargetFrameworkVersion) for Xamarin.Forms.Platform.dll (v7.1) is 
greater than the $(TargetFrameworkVersion) for your project (v6.0). 
You need to increase the $(TargetFrameworkVersion) for your project.

The $(TargetFrameworkVersion) for Xamarin.Forms.Platform.Android.dll (v7.1) 
is greater than the $(TargetFrameworkVersion) for your project (v6.0). 
You need to increase the $(TargetFrameworkVersion) for your project.

The $(TargetFrameworkVersion) for FormsViewGroup.dll (v7.1) is greater 
than the $(TargetFrameworkVersion) for your project (v6.0). 
You need to increase the $(TargetFrameworkVersion) for your project.

The warning says I need to increase the TargetFrameworkVersion for my Android project, but when I look at the properties for this project, I actually can’t increase it past version 6 (MarshMallow).

screenshot.1

Fortunately we’re not at a dead end here – we can go to the Start Menu, and search for the “SDK Manager” for Android, which is installed with the Xamarin component of Visual Studio 2017 (shown below).

screenshot.2

Tip: Run the Android SDK Manager as administrator by right-clicking on the shortcut and select “More -> Run as administrator”. If you don’t run as administrator, you might get an error later when the program tries to create a temporary folder for downloads.

When I start the Android SDK Manager, it analyses the packages presently installed, and advises what needs to be updated. On my system, 10 packages needed to be installed or updated, as shown below.

screenshot.3

When I click on the “Install 10 packages…” button, another window appears asking me to accept the licence. I accepted the licence and clicked on “Install”.

screenshot.4

The installation and update procedure starts – this can take a few minutes.

screenshot.5

Once it’s finished installing, let’s return to Visual Studio 2017  –  I restarted it, and then cleaned and rebuilt the solution. This time the warnings about $(TargetFrameworkVersion) mismatches are gone.

Getting rid of warning IDE0006 – “Error encountered while loading the project”

I sometimes found that I had a warning IDE0006 which advised “Error encountered while loading the project. Some project features, such as full solution analysis for the failed project and projects that depend on it, have been disabled“.

screenshot.6

This usually happened just after I created a project, and I found that if I close VS2017, restart it, and re-open and rebuild the solution the warning disappears.

So to summarise, in order to compile the default project without errors or warnings:

  • Run the Android SDK manager as administrator, and install/update the recommended packages.
  • Restart Visual Studio 2017 and re-open the project.
  • Close all files from the shared project which have the type *.xaml.cs.

Running in your application in the Windows Phone Emulator

Tip: If you want to run your application in an emulator, you’ll definitely need a 64-bit machine which allows hardware virtualisation. Your machine will also need to be pretty powerful, or you might find running an emulator to be unbearably slow.

I found this to be straightforward as soon as I installed a Windows Phone emulator.

If you don’t have any Windows Phone emulators installed, you can grab some from here: https://developer.microsoft.com/en-us/windows/downloads/sdk-archive

I changed the start-up project to the UWP project, and changed the debugging target to be one of the Windows Phone Mobile emulators.

screenshot.14

After hitting play (or F5) to start running the Windows UWP application in a Windows Phone emulator, I was prompted to set my machine into Developer mode to allow me to load apps – I just had to select the third option (“Developer mode”) as shown in the image below (you can access this screen from Start -> Settings -> For developers).

screenshot.20

But after changing this setting, everything worked well – no gremlins here. The phone emulator starts up after a couple of minutes, and I was easily able to see the Xamarin application in the list of apps installed to the phone emulator.

screenshot.39

And when I run the Xamarin app in the emulator, I see the correct result – a simple form with a message saying “Welcome to Xamarin Forms!”

screenshot.40

Running your application in the Android Emulator

Visual Studio 2017 comes packaged with several Android Emulators – you can see them if you change the target project to the Android one, and look at the dropdown list on its right.

screenshot.15

Tip: I never use either the two emulators which target ARM. I have never managed to successfully deploy a project to one of these emulators, even with a reasonably powerful machine.

If you don’t believe me, Visual Studio even gives you a warning if you try to use them:screenshot.25

It’s much, much faster to target one of the emulators which targets x86.

Use the Android x86 emulator – but you need to turn Hyper-V off

You don’t need to uninstall Hyper-V to run the Android x86 emulator on Windows 10 – you just need to turn it off. The command to do this is very simple from a command prompt running as administrator:

bcdedit /set hypervisorlaunchtype off

Reboot for this setting change to take effect.

Of course it might not suit you to turn Hyper-V off on your machine – and another alternative is to deploy to an actual Android device – there’s some great instructions for this here: https://developer.xamarin.com/guides/android/getting_started/installation/set_up_device_for_development/

My experience was that I couldn’t successfully start and deploy my project to an Android emulator from Visual Studio 2017. However, I was able to start the Android emulator from the Android AVD Manager, available from the start menu (as shown below).

screenshot.16

When you start this program, you’ll see a dialog like the one below which lists the Android Virtual Devices available on your development machine.

screenshot.17

Select one of the x86 emulators, and click on the “Start…” button. Accept the default options on the launch screen, and an Android phone emulator will start.

screenshot.18

Now go back to Visual Studio 2017. Select the emulator which you’ve just started in the drop down list on the right of the green “Play” arror. Now right click on the Android project, and select “Deploy Solution”.

This should now deploy the Xamarin application to the Android emulator, as shown below (our app is in the top row, second column):

screenshot.19

And when we click on the Xamarin application icon in the emulator, as expected we see the same screen as in the Windows Phone emulator which says “Welcome to Xamarin Forms!”

screenshot.20

Wrapping up

This was just a quick post to help other Xamarin developers who are starting out avoid some of the headaches I had. And just to be really clear, I’m not criticising Xamarin or Visual Studio – getting code to work on 3 different platforms which are constantly changing and updating is pretty miraculous, and ultimately the things I had to do weren’t that big a deal to change.

There are already a few troubleshooting guides from Microsoft on Xamarin like this one. The tips below are things I didn’t find covered anywhere else.

  • Sometimes errors (for example, CS0103) are mis-reported by VS2017 for files which are open in the editor, particularly *.xaml.cs files from the Shared Project – try closing these files and rebuilding to see if the errors go away.
  • Other warnings appear after the project is first created (for example, IDE0006), but if you restart VS2017 and re-build the project, the warning disappears.
  • Opening the Android SDK Manager as administrator and updating the libraries you have on your development machine can help to remove warnings related to TargetFrameworkVersion mismatches – remember to restart VS2017 after the update, and then clean and rebuild your solution through VS2017.
  • Don’t use the Android ARM emulators on Windows 10 – instead start an x86 emulator from the Android AVD manager, and deploy from VS2017 to the emulator which is running.
  • If the x86 emulator won’t start, you might need to disable Hyper-V using the command “bcdedit /set hypervisorlaunchtype off“.

About me: I regularly post about .NET – if you’re interested, please follow me on Twitter, or have a look at my previous posts here. Thanks!

2 thoughts on “Troubleshooting the default install of Xamarin with Visual Studio 2017 and Windows 10 Creators Update

Comments are closed.