Program analysis for XAML

Xamarin.Forms is a great platform for building a wide range of apps. XAML and data binding make layouts a lot clearer than writing it in C#, and it saves a ton of code. But the development process has some gotchas that wouldn’t be there in a pure C# app.

When working in C#, you get a lot of help from the language and IDEs keeping you from making a bunch of kinds of mistakes. Type safety stops a lot of potential bugs from making it past the Compile step. The refactoring support for C# is pretty slick as well. Move code around, rename stuff, change namespaces, and it all gets sorted out quickly.

But when working in XAML, that’s mostly gone. There isn’t a lot of checking that happens during the build, even with XAML compilation turned on. Bugs in XAML end up getting debugged at runtime. This means a much slower bug fix process than an equivalent bug in C# would have. Some of these bugs end up causing crashes, so they’re pretty easy to spot once you run your app and get to the right page. Others, like mistyping identifier names in Bindings, can just silently fail.

As a simple example, let’s compare the mistake of putting resources directly inside <ContentPage.Resources> and forgetting to wrap in a <ResourceDictionary>. This buggy code in a XAML file:

    <ContentPage.Resources>
        <Color x:Key="MyColor">#123456</Color>
    </ContentPage.Resources>

Will build just fine, and depending on the resources, the app could fail in a number of ways at runtime. Try something similar in C#:

Resources = new KeyValuePair("MyColor", Color.Yellow);

And the app won’t even build.

XamRight

I figured there had to be a better way, some way to use XAML and databinding, and also get the kind of type safety and typo catching that I rely on with C#. From personal conversations, reading forums.xamarin.com and stackoverflow, I’m pretty sure I’m not the only one who could benefit from this checking.

After spending a lot of time wading into Visual Studio extensibility, I’m happy to announce that XamRight is just about ready for beta testing! It’s a Visual Studio extension that gives you those little squiggly lines to tell you something’s wrong while editing your Xamarin.Forms XAML. Install it, and it’ll start doing its thing. You can also have it analyze your entire project, just to make sure everything is clean before you commit.

Aside from a parser that understands the structure of XAML, It’s got a fast, custom-built static analyzer to figure out BindingContexts, ListView.ItemsSource, and other key pieces of information needed to correctly understand how the Xamarin runtime will evaluate your XAML files. While it isn’t possible to automatically deduce all BindingContexts in every program, it’s already been used effectively on several apps.

While there is still quite a backlog of features that we plan to add to XamRight, it has already makes app development easier for us, so we can deliver high quality apps quicker and more reliably for our clients.

Here’s a four minute demo showing how it works on the Xamarin CRM app.

Interested in giving it a try? Contact us for access to the beta.

Author

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.