Categories
.NET .NET MAUI Android Blazor Code Deep Dive Desktop Developer Extensions Hybrid iOS Mobile MVVM Templates Visual Studio Windows WinUI 3 Xamarin

Introducing .NET MAUI Generic Item Templates for Visual Studio 2022

To achieve more in less time, the revolutionary generic template is now within VS2022 itself, without ever having to switch between the IDE and the Terminal.

Last week introduced the .NET MAUI Generic Item Templates for CLI. Since switching between the IDE and the Terminal is always a constraint. So decided to implement the revolutionary generic templates within the IDE itself so the experience is quite delightful. The right tools and techniques result in improved productivity. Achieve more in less time.

Published as a VS extension, starting v4.9.0.0

To install, either download and install from VS Marketplace or use the built-in Extension Manager (shortcut: Alt+X+M) from within the Visual Studio IDE, search with maui keyword with the Online Tab in focus and look for .NET MAUI Project and Item Templates.

.NET MAUI Project and Item Templates - Visual Studio Extension by Vijay Anand E G
.NET MAUI Project and Item Templates – Visual Studio Extension

To know more about other features supported in this All-in-One templates pack, read this article.

Salient Features:

  • A generic template to create items of any type
  • Available on both XAML and C#
  • Both need one required input, the Base type
  • And optionally takes another input, to specify the Generic base type
  • In addition, the XAML template takes one more input (Xaml Only), to generate only the XAML definition
  • These inputs are defined in the UI as DropDown, TextBox and CheckBox for ease of use
  • Frequently used base types are loaded in the Editable dropdown, user can also enter their value here
  • Apart from xmlns prefix, value for both Base and Generic Type is to be entered in Pascal notation
  • XAML templates support XML namespace prefix, quite like how it is used in the real world (mct:Popup)
  • Another big advantage of using this feature on IDE is the relative namespace to the folder where the item is created whereas on CLI, this defaults to the root namespace (a limitation in the CLI templating engine)

Note: Namespace resolution in both XAML and C# files is left to the user as deriving them with the template is outside its scope.

Tip 1: For the XAML template, pass the xmlns scope as part of the input value and it’ll be used appropriately in the generated source files.

Tip 2: Use local, (a predefined scope in the template) to refer to the types in the same directory like Views. For example, local:BasePage

So let’s dive deeper to catch some action in real-time. Screenshots are there to fill in with more details.

First, right-click on the Project item or any Sub-folder below, then choose the Add option (an expandable sub-menu), and then choose the New Item … option. Alternatively, use the shortcut Ctrl+Shift+A.

Visual Studio 2022 - Add New Item option
Visual Studio 2022 – Add New Item option

This will open up the Add New Item dialog, where all the installed item templates will be listed. We’re interested in the MAUI templates.

So, select the MAUI group in the left side Tree view (highlighted), and in the middle pane, the Generic Item template is listed on the top as once you’re used to it you’ll no longer need other templates.

Visual Studio 2022 - Add New Item dialog - .NET MAUI Generic Item in Focus
Visual Studio 2022 – Add New Item dialog – .NET MAUI Generic Item in Focus

Then in the bottom pane, provide a meaningful name to the item that is to be created.

Click Add to open the Generic Item custom dialog.

.NET MAUI Generic Item dialog - Part of All-in-One Templates Pack by Vijay Anand E G
.NET MAUI Generic Item dialog

Here, the Base type is an Editable dropdown, loaded with some useful predefined values. If the expected value is not available, enter your own value here. As you enter, the predefined options are suggested (case-sensitive), or open the dropdown to select one (shortcut: F4).

The Generic base type is a TextBox where the user can enter the type parameter and is optional.

Apart from xmlns prefix, values of both Types are validated to be in Pascal notation like Page, Popup.

For XAML templates, the Xaml Only CheckBox is shown to generate the XAML definition alone, if opted. Useful for ResourceDictionary.

OK is the default action, so pressing the Enter key with focus in any of these controls will trigger the validation associated with this.

Pressing the Escape key will close the dialog thereby canceling the Add New Item action as a whole.

Once validation is through, this will create the source files with the opted-in values filled in the right places. As already mentioned, the namespace resolution in both XAML and C# files is left to the user as deriving them with the template is outside its scope. Suggest making use of code snippets and global usings thereby minimizing the work to a greater extent.

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 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 the word with your team and inner circle.

And a considerable amount of my time is spent on adding new features to the templates pack (this custom dialog and associated logic are ingenious work) 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.

By Vijay Anand E G

A software professional with over a decade long industry experience in developing products that spans across desktop, mobile, and web.

2 replies on “Introducing .NET MAUI Generic Item Templates for Visual Studio 2022”

Comments are closed.