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
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.
What do we need to get Roslyn going? The easiest way is to use the current Azure Visual Studio 2015 VM. Additionaly we need:
- The Visual Studio SDK: http://go.microsoft.com/?linkid=9863867
- The Visual Studio SDK Templates: It contains the new Diagnostic and Code Fix Project Templates, https://visualstudiogallery.msdn.microsoft.com/849f3ab1-05cf-4682-b4af-ef995e2aa1a5
- The Syntax Visualizer: This tool is invaluable to find the right statements in code navigation! https://visualstudiogallery.msdn.microsoft.com/70e184da-9b3a-402f-b210-d62a898e2887
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
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 ...