Categories
.NET .NET 8 .NET 9 .NET MAUI Android Automation Blazor C# Desktop Developer General Getting Started Hybrid iOS macOS Mobile Preview Templates VS Code What's New Windows Xamarin Xamarin.Forms

Elevating .NET MAUI Item Templates: Simplified Namespace Resolution in Latest Release

Back in February, an article was published on the efficient utilization of Item Templates through the use of script automation. The article, titled .NET MAUI – Item Templates – Made Easy on CLI and VS Code, provides a detailed focus on this topic.

That article addressed two issues:

  • Better namespace resolution
  • Do more with fewer keystrokes

Now, the namespace resolution issue has been partially supported within the .NET SDK, eliminating the requirement for additional script files. A PR was submitted to the .NET Templating source repository, underwent review, and was subsequently merged into the main branch. This enhancement was then included in the release of .NET 9 Preview 6.

This pull request is intended to obtain the current directory path, which is accessible via the WorkingDirectory bind symbol, from where the dotnet new command is being invoked. Previously, this functionality was managed from the script file by setting the environment variable of the same name. Now, this is from within the .NET SDK itself 😊.

By joining the root namespace and the relative path derived from this property to the project file location, template authors can deduce the correct namespace for the item being generated. Both the root namespace and the project file location are accessible via the MSBuild property.

With custom batch script:

maui-page Settings
class-cs BaseViewModel ObservableObject

Typical CLI command:

dotnet new maui-page -n Settings
dotnet new class-cs -n BaseViewModel -b ObservableObject

It is imperative to ensure that the project is restored before attempting to utilize the commands.

From .NET 9 Preview 6 onwards, both of the aforementioned commands will yield a comparable output for the primary-level folder structure.

The primary benefit of this update is that the All-in-One .NET MAUI Item Templates are now genuinely cross-platform, allowing use with any CLI shell, and are no longer limited to PowerShell Core on non-Windows platforms.

Further exploration is required to ascertain the string functions that are supported to ensure functionality at any level.

Install the latest version of the .NET MAUI All-in-One Templates Pack for CLI from NuGet.

dotnet new install VijayAnand.MauiTemplates

If you find these templates and extensions useful, kindly support the work by adding your review to the VS Marketplace and also by adding a Star to the GitHub repository where it’s actively maintained. Also, share this article with your team, inner circle, and social media channels.

And a significant portion of my time is dedicated to incorporating new features into the templates pack. If you appreciate the work and wish to support the ongoing momentum, please consider sponsoring the project on GitHub here. Your support means a lot!

Happy coding. Stay connected as we continue to learn and share the experiences from this exciting journey of being a .NET developer.

One reply on “Elevating .NET MAUI Item Templates: Simplified Namespace Resolution in Latest Release”

Comments are closed.

Discover more from Developer Thoughts

Subscribe now to keep reading and get access to the full archive.

Continue reading