Fix broken .NET MAUI templates
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):
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:
All newly created files use tabs for indentation:
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.
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:
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:
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:
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:
When I did that, I noticed that the project file also used tabs instead of spaces for indentation:
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).