Categories
.NET .NET MAUI Android Blazor Desktop Developer Hybrid iOS Mobile NuGet Templates What's New Windows Xamarin

Introducing .NET MAUI Generic Item Templates

Create items of any type from a single template. Available for both XAML and C#.

Working with the .NET MAUI All-in-One Templates Pack is cool. To make it even cooler, now introducing the revolutionary .NET MAUI Generic Item Templates. Available for both XAML and C# on CLI. This feature is now available with the v4.8.0. Update now.

Update: This generic template is now available within VS2022 itself. For more details, take a look at this article.

With these generic templates:

  • Create items of any type
  • It is named as maui-item and maui-item-cs
  • Both need one required parameter, -b / --base, the base type
  • And optionally takes another parameter, -g / --generic, to specify the generic base type
  • In addition, the XAML template takes one more parameter, -xo / --xaml-only, to generate only the XAML definition

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

Pass the xmlns scope as part of the input parameter value and it’ll be used appropriately in the generated source files.

Example 1:

dotnet new maui-item -n ThemePopup -b mct:Popup

Then open the XAML file and define the xmlns for mct.

This creates a Popup from the .NET MAUI Community Toolkit. No more using other item templates and then editing them in multiple places.

Output:

<?xml version="1.0" encoding="UTF-8" ?>
<mct:Popup
    x:Class="MyApp.ThemePopup"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:d="http://schemas.microsoft.com/dotnet/2021/maui/design"
    xmlns:local="clr-namespace:MyApp"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
</mct:Popup>
namespace MyApp
{
    public partial class ThemePopup : Popup
    {
        public ThemePopup()
        {
            InitializeComponent();
        }
    }
}

Example 2:

dotnet new maui-item -n SearchPage -b vw:MauiPage -g vm:SearchViewModel

Then open the XAML file and define the xmlns for vm and vw.

Creates a Search page inheriting from the generic MauiPage (that resolves the instance of the ViewModel and then assigns it to the BindingContext – Available as part of VijayAnand.MauiToolkit).

Output:

<?xml version="1.0" encoding="UTF-8" ?>
<vw:MauiPage
    x:Class="MyApp.SearchPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:d="http://schemas.microsoft.com/dotnet/2021/maui/design"
    xmlns:local="clr-namespace:MyApp"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    x:TypeArguments="vm:SearchViewModel"
    mc:Ignorable="d">
</vw:MauiPage>
namespace MyApp
{
    public partial class SearchPage : MauiPage<SearchViewModel>
    {
        public SearchPage()
        {
            InitializeComponent();
        }
    }
}

Example 3:

dotnet new maui-item -n BasketItemTemplate -b SwipeView

Creates a new item that inherits from SwipeView.

Output:

<?xml version="1.0" encoding="UTF-8" ?>
<SwipeView
    x:Class="MyApp.BasketItemTemplate"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:d="http://schemas.microsoft.com/dotnet/2021/maui/design"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:MyApp"
    mc:Ignorable="d">
</SwipeView>
namespace MyApp
{
    public partial class BasketItemTemplate : SwipeView
    {
        public BasketItemTemplate()
        {
            InitializeComponent();
        }
    }
}

These are some real-world samples. Try them and share your feedback.

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 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 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”

Comments are closed.

Discover more from Developer Thoughts

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

Continue reading