Fix broken .NET MAUI templates

September 9th 2022 .NET MAUI Visual Studio

When I started working with .NET MAUI, I immediately noticed that the code files generated by the project template use tabs instead of spaces for indentation (unlike all other .NET project templates):

Tabs in .NET MAUI project template

This bothered me, but fortunately it can be easily fixed by using Edit > Advanced > Format Document (or Ctrl+K, Ctrl+D as default keyboard shortcut).

However, I soon discovered that all .NET MAUI file templates have the same problem:

.NET MAUI file templates

All newly created files use tabs for indentation:

Tabs in .NET MAUI file template

I did not like having to manually trigger the Format Document command for each new file, so I started looking for alternatives. First, I came across the Code Cleanup On Save Visual Studio extension. It does exactly what its name implies. However, it turned out that this extension is no longer needed. In the newer versions of Visual Studio 2022, the same functionality can be enabled with the Run Code Cleanup profile on save setting, which is hidden under Options > Text Editor > Code Cleanup.

Run Code Cleanup profile on save

Unfortunately, the tabs were still not replaced with spaces when saving the file, even though I had already configured Visual Studio to use spaces for indentation:

Insert spaces for indentating C# code

The key to the puzzle was the sentence at the bottom of this settings page: I had to uncheck the Use adaptive formatting check box at Options > Text Editor > Advanced:

Use adaptive formatting

This finally replaced the tabs with spaces when saving the file.

It did not take long, however, to realize that there were no nullable reference types warnings in my code:

Missing warnigns for nullable reference types

The reason, of course, was that nullable reference types were not enabled in the project file. I believe that this is the only .NET 6 project template where they are not enabled by default. It is quite easy to enable them: just add the following to the project file:

<Nullable>enable</Nullable>

When I did that, I noticed that the project file also used tabs instead of spaces for indentation:

Tabs in .NET MAUI project file

Since there is no code cleanup support for XML files, saving the file did nothing, so I had to use the Format Document command (Ctrl+K, Ctrl+D) again. I could live with that, since it is a one-time action (for each project, of course).

I do not know why .NET MAUI templates, unlike all other .NET project templates use tabs for indentation. I like consistency in my code, but fortunately the problem is easily fixed with the tools available in Visual Studio 2022. Running appropriately configured code cleanup on save solved the problem for me, but it can do much more. It's the closest thing to Prettier for C# code that I know of (and I can not imagine working on TypeScript or JavaScript projects without Prettier). I recommend reading more about code cleanup in Visual Studio to learn how to best use it.

Get notified when a new blog post is published (usually every Friday):

If you're looking for online one-on-one mentorship on a related topic, you can find me on Codementor.
If you need a team of experienced software engineers to help you with a project, contact us at Razum.
Copyright
Creative Commons License