Categories
.NET .NET MAUI Android Automation Blazor Deep Dive Desktop Developer General Hybrid iOS Mobile MVVM NuGet Templates Visual Studio VS Code What's New Windows Xamarin Xamarin.Forms

.NET MAUI – Item Templates – Made Easy on CLI and VS Code

Better namespace resolution and Script automation

Even though the All-in-One item templates have the same set of features in VS2022 and on the CLI. The CLI has its own limitations and here are the notable ones:

  1. The namespace resolution is not on par with its VS counterpart
    • On CLI – Defaults to the root namespace
    • Whereas in VS to the exact folder where the item is created
  2. A lot of (repeated) typing is required
    • The underlying reason is CLI’s named parameters design

The namespace issue requires some support from the underlying templating engine to resolve it once and for all. For now, have provided a solution in the larger interests, based on the commonly used folder conventions that group the program chunks in a logical folder based on their use like Views, ViewModels, Models, and so on. One level from the root.

The second issue is that the parameters defined in the CLI templates require a prefix for all cases, even the mandatory item name requires a prefix that qualifies it to be a name (-n / --name).

dotnet new maui-page -n Settings

An ideal solution to this issue is using Positional Parameters. For example, one that processes the keyword that immediately follows the template type as its name. With this option, even typing dotnet new again and again, might seem like a repeated action.

Design:

Taking all these into account, introducing the Script files (in Batch and PowerShell format), that support positional parameters, for the .NET MAUI items and Partial Class (C#) templates made available with the All-in-One template pack on CLI.

The design is as follows, the mandatory inputs are positional and any other inputs are passed to the underlying dotnet new command as received by the script file (example below 2 & 6).

For every item template, a name is required, so that’s the first parameter. Based on the template type there can be one or more positional parameters in addition (example below 3 to 5). .NET MAUI Item and Partial Class (C#) to be specific.

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

dotnet new install VijayAnand.MauiTemplates

Download the script files with direct links: Batch file and PowerShell file. Also made available in the Artifacts folder in the GitHub repository. Have uploaded it as a zip file to avoid any potential download issue.

Unpack it to the folder to which the user has access like the user profile folder like %UserProfile%\Utility. (For non-Windows users, this may correspond to $HOME and the path to $HOME/Utility). Then add this path to the Path environment variable (steps detailed for Windows OS), so that the script files are accessible to the logged-in user from the CLI anywhere.

Open the Environment Variables dialog from Windows Run using the below command:

rundll32 sysdm.cpl,EditEnvironmentVariables

In the dialog, the top section is for the user level, and the bottom section is for the system level. In the top section, scroll through the items and find the Variable named Path, click the item so that it is highlighted, then click on the Edit action button.

This will open up the Edit environment variables dialog, click on New action from the right-side list. This will open up an editable text field at the fag end of the list, type the folder path where script files are copied and press Enter to persist the change. Finally, click OK on both dialogs to close them. Don’t attempt to modify other things, doing so may alter the OS behavior altogether. Restart the terminal window for the change to take effect.

For non-Windows users, recommended to install PowerShell Core and make use of the PS scripts that host the same features as the batch script. Windows users can also try this to go cross-platform seamlessly.

Auto distribution of these script files with the template pack itself in a future version is subject to feasibility as it requires some more research.

Usage:

For now, have provided the script files of the same name as the template type name to get started. But this can even be shortened at your own convenience avoiding any ambiguity.

1. A .NET MAUI Page:

maui-page Settings

2 A (Xaml Only) Resource Dictionary:

maui-resdict DarkTheme -xo

3. A Popup using the generic item template:

maui-item ThemePopup mct:Popup

4. A ViewModel initialized page:

maui-item CartPage vw:MauiPage vm:CartViewModel

5. A class with a base type (with file-scoped namespace opted-in):

class-cs BaseViewModel ObservableObject -fsn

6. A static class (used for markup definition):

class-cs ViewExtensions -st

To know the parameters list, type use the below command format:

  • For Batch script, invoke the template type (without any parameters)
    • maui-page
  • For PS script: Use the built-in get-help command
    • get-help maui-item

Documentation is available here. If you find any issues or features to suggest, feel free to raise an issue with appropriate details here. Ideas are most welcome.

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 cooked. Also, share this article with your team, inner circle, and social media channels.

And a considerable amount of my time is spent on adding new features to the templates pack if you would like to recognize the work and sustain the momentum, kindly consider sponsoring the project in GitHub here. Thanks in advance.

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 “.NET MAUI – Item Templates – Made Easy on CLI and VS Code”

Comments are closed.