The Roslyn Report: Writing Diagnostic Analyzers for Xamarin - Part 1

The Task:

One of the most important thing when developing iOS and Xamarin are the UIView and the UIViewController with their numereous events. All the wiring of logic and UI and also the layouting happens here. Even when you use Frameworks like MVVM or XIBs you are dependant to the flow of LoadView and ViewWillDisappear. Adam Kemp has an excellent blog describing what to do on which stage of the iOS Layout process: http://blog.adamkemp.com/2014/11/ios-layout-gotchas-and-view-controller.html

But how can one controll the code to obey the rules Adam described in his blog? Enter Roslyn - the new Microsoft compiler engine with the possibility to write own code diagnosis routines! See the github page for additional doku and the MSDN site for a reference.

The Setup:

What do we need to get Roslyn going? The easiest way is to use the current Azure Visual Studio 2015 VM. Additionaly we need:

After the setup you should have a running VS2015 with this new project template:

When you create a project you get a solution with the following structure:

  • Analyzer (Portable): The analyzer
  • Analyzer.Test: Unit tests for the analyzer
  • Analyzer.Vsix: A Visual Studio installer for the analyzer.

A diagnostic for Xamarin

Because Roslyn analyses code fragments with partial compilation the Xamarin.iOS.dll from C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Xamarin.iOS\v1.0\ is needed to compile and diagnose Xamarin code fragments. Copy it over from your development environment to the Azure VM. I would suggest to copy it to the same folder again. Then reference it from the Unit Test project to get it into build output directory and make it loadable by Roslyn.

In the next entry I will show how to write a Roslyn code analysis ...

Azure PowerShell - an easier way to install

The standard Microsoft description (http://azure.microsoft.com/en-us/documentation/articles/install-configure-powershell/) leads you to an Web Installer, which also installs IIS on your client computer. That's a very wide footprint for a shell lib and in many cases not doable.

A much simpler approach is the Microsoft MSI on the Azure Github (https://github.com/Azure/azure-sdk-tools/releases). Just install the "Windows Standalone" MSI and import the Azure module in your PowerShell environment:

Import-Module "${Env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Azure.psd1"

And you're done ...

iOS on iPads is different - sometimes

The usage of an UIActivityViewController in iOS/Xamarin is pretty simple. A few lines of code and you get following:

See the gist below for the source code. The usage comes down to:

var activityViewController = new ActivityViewController("Hello world"); activityViewController.PresentActivityViewController(this, true);

But there is a little caveat: On iPads you must anchor the overlay to the button, which opened it. And thats only in iOS 8. So watch out for line 52. We dont have this often in Xamarin / iOS, but it happens.

Happy coding!