Thursday, February 03, 2022

Don't give up on XAML yet.

Some people aren't fans of XAML. That's ok.

Some people use it because they have to.

A very small number of people think deeply about XAML. 

I include myself in the third group.

some random XAML in an editor

There's a popular trend at the moment to dismiss XAML because of its limitations. 

The argument goes that you should use C# (or equivalent--hello VB fans) to define the whole of the application, rather than use a separate tool (XAML) to define the user interface. This is seen as a way to simplify a codebase and avoid some of the limitations of XAML.


C# is a general-purpose language. You can use it for everything but that doesn't mean you should. XAML, while not a full programming language is designed specifically for defining user interfaces and has appropriate tooling available.

XAML is not a "proper" (Turing-complete) language but it doesn't need to be. It can still do useful and powerful things.

Most of the arguments I'm hearing for just using C# come down to the idea that the tooling for defining UI's in C# should be made better so that people can use that instead of having to use XAML.

I have concerns about this approach:

  • It ignores the possibility of improving the XAML experience, which is the ultimate source of the issue.
  • It means the need for more work in supporting (developing & maintaining) the ability to create UIs with XAML and C#.
  • It indicates that there is no plan, desire, or intention to improve the XAML tooling and experience. Should people, therefore, infer that XAML is in a fixed state and will never be touched again. The consequence of such an idea is that people should stop using XAML. What then for all the existing XAML code that exists? There's an implication that this should probably end up being rewritten in C#. This means that we don't have to jump very far to get from some people being disappointed with XAML to everyone having to rewrite their code so they don't use it.
  • It can be harder to visualize a UI from just the C# code. (I recognize that this is just my current opinion though.)
  • Separation of concern boundaries are much more easily broken when everything is in one language.
  • Just because XAML isn't a "proper" or full (turning-complete) language is not a reason not to use it. Making changes to a general-purpose language (C#) to make it easier to use for a specific scenario seems equally flawed. In fact, when XAML is treated like a "proper" language it becomes much easier to write, read, and maintain that code. (More on this in a future post.)

I will, of course, admit that creating UIs in C# can (& does) make some things easier to read and understand. However, I don't think that should automatically mean we do away with everything that isn't C#. Also, I'm yet to see very complex UIs defined in C# but have seen many complex scenarios implemented in XAML. 

This isn't to say that complex UIs can't be created in C# as well as XAML, it's more to highlight that the situation is complex. It's very easy to reduce this discussion based on personal experiences but the reality is that XAML and C# are used by a very large number of people to build massively varied types of software with vastly differing specifications, requirements, and uses. There are a minuscule number of absolutes that can be relied upon when discussing the options and what's "best" for everyone.


Where we're at now, things are improving for UIs defined in C#.

But, why not also improve them for XAML too?


Yes, XAML has been stagnant for many years and the plans for a XAML Standard ultimately came to very little. 

Rather than complain about the limitations of XAML (& current tooling) instead let's look at how things could be better.

As you might expect I'm doing some tinkering myself with regards to Visual Studio extensions that can be useful, but the XAML-related capabilities are almost entirely without the ability to customize or extend them.

I know it's not reasonable to expect big changes to be made quickly but I don't think it's unreasonable to ask Microsoft to consider improving the tooling that's currently available. So, I'm starting with a very basic suggestion: Add support for compiler directives in XAML. VS/WPF request, WinUI request, .NET MAUI request. (Yes it's sad that there isn't a single place to do this.)

It'd be great to see some more people add thoughts to those proposals. <hint /> 😉




1 comment:

I get a lot of comment spam :( - moderation may take a while.