Quite like the .NET MAUI All-in-One Templates Pack, there’s a similar template pack for Xamarin.Forms too and is available as both a CLI Template and a VS Extension. Here’s the command to install the CLI part.
dotnet new install VijayAnand.FormsTemplates
To install as VSIX, download and install from VS Marketplace or use the Extension Manager (shortcut: Alt+X+M
) from within the Visual Studio IDE, search with xamarin.forms templates
keyword with the Online
Tab in focus and look for Xamarin.Forms Project and Item Templates.
The pack hosts a project template and a bunch of item templates:
- Xamarin.Forms Class Library
- It is named as
formsclasslib
- It is named as
This supports creating a library project with the Target Framework as .NET Standard 2.1
or .NET Standard 2.0
(the default value).
On CLI, available as a parameter -f
/ --framework
and within the IDE, as a drop-down list to select.
dotnet new formsclasslib -o FormsLib
It supports including the commonly used packages:
- Xamarin.Essentials
- Xamarin.Essentials.Interfaces
- Xamarin.CommunityToolkit
- Xamarin.CommunityToolkit.Markup
- CommunityToolkit.Mvvm
And option to reference VijayAnand.Toolkit.Markup (aka SharedToolkit) (This makes Bindings easy in Coded UI, check this article for more details) and supports migrating to .NET MAUI as equivalent APIs are mostly available.
There is a single parameter to include all these packages (except Xamarin.Essentials.Interfaces, an explicit option):
-asp
/ --all-supported-packages
-iei
/ --include-essentials-interfaces
dotnet new formsclasslib -o FormsLib -asp -iei
Item Templates:
Ensure the project is restored before making use of the item templates.
For all the item templates, the type and file extension need not be included as part of the value of the name parameter -n
/ --name
as auto type suffixing feature is supported (from v1.6.0 and above).
The below command would generate the type named SearchPage
and the source files SearchPage.xaml
and SearchPage.xaml.cs
dotnet new forms-page -n Search
Similarly while working with the MVVM templates, don’t need to suffix Page/ViewModel to the name, it’ll be automatically included.
ContentPage and ContentView item templates are made available only in the CLI as pre-built templates for them are installed in Visual Studio with the Xamarin workload itself.
- ContentPage (XAML and C#)
dotnet new forms-page -n Settings
dotnet new forms-page-cs -n Search
- ContentView (XAML and C#)
dotnet new forms-view -n Order
dotnet new forms-view-cs -n Product
- ResourceDictionary (with and without code behind)
dotnet new forms-resdict -n DarkTheme
dotnet new forms-resdict -n LightTheme -xo
- Shell (XAML and C#)
dotnet new forms-shell -n App
dotnet new forms-shell-cs -n Mobile
- ContentPage and ViewModel (XAML and C#)
dotnet new forms-mvvm -n Cart
dotnet new forms-mvvm-cs -n Profile
- Generic Item (discussed in detail below)
dotnet new forms-item -n ThemePopup -b xct:Popup
dotnet new forms-item -n CartPage -b vw:FormsPage -g vm:CartViewModel
dotnet new forms-item-cs -n CardTemplate -b Grid
Generic Item Templates:
- A revolutionary generic item template, in XAML and C#, for creating items of any type
- Supported in both VS2022 IDE and CLI
- On CLI, it is named as
forms-item
andforms-item-cs
- In the VS New Item dialog, find them under
Xamarin.Forms\All-in-One
folder (refer to the screenshot below) - The same set of parameters is defined in the UI as
Dropdown
,TextBox
andCheckBox
for ease of use - Both need one required input, the Base type (
-b
/--base
) - And optionally takes another input, to specify the Generic base type (
-g
/--generic
) - In addition, the XAML template takes one more input, to generate only the XAML definition (
-xo
/--xaml-only
) - 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 (
xct: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)
Select the template type, then enter its name, and click Add to bring the below custom dialog to provide details of the item you’re about to create.
CLI Examples:
- A Popup using Xamarin.CommunityToolkit
dotnet new forms-item -n ThemePopup -b xct:Popup
- A Page that inherits from a generic page that instantiates the ViewModel and then assigns it to the BindingContext.
dotnet new forms-item -n CartPage -b vw:FormsPage -g vm:CartViewModel
public class FormsPage : ContentPage {}
// Generic base class
public class FormsPage<TViewModel> : FormsPage
where TViewModel : class, INotifyPropertyChanged, new()
{
public FormsPage()
{
ViewModel = new TViewModel();
BindingContext = ViewModel;
}
public TViewModel ViewModel { get; protected set; }
}
- A custom Grid that’s designed like a Card
dotnet new forms-item-cs -n CardTemplate -b Grid
Note: Namespace resolution in both XAML and C# source 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
And help is always available around the corner. Use the parameter -h
/ --help
on CLI and as tooltips in the IDE. For example:
dotnet new formsclasslib -h
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 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 “Introducing Xamarin.Forms Templates”
[…] Introducing Xamarin.Forms Templates (Vijay Anand E G) […]
LikeLike