Skip to content

egvijayanand/xamarin-forms-templates

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Xamarin.Forms Project and Item Templates for building native apps for iOS, Android, UWP, macOS, Tizen from a single, shared C# codebase.

Join me on Developer Thoughts, an exclusive blog for Xamarin.Forms, .NET MAUI, and Blazor with articles on working with it.

Available to install from

NuGet VS Marketplace
VijayAnand.FormsTemplates - NuGet Package Xamarin.Forms Project and Item Templates - VS Marketplace

Access within Visual Studio IDE

Installation:

Install from VS Marketplace

Project Template Listing:

Xamarin.Forms Class Library Project Template

Project Options:

Xamarin.Forms Class Library - Project Options

Item Templates:

Xamarin.Forms - Item Templates

Access via CLI

To install the template NuGet package, use the below .NET CLI command:

dotnet new install VijayAnand.FormsTemplates

If you've already installed this package, then it can be updated to the latest version with the below command:

dotnet new update --check-only
dotnet new update

Templates Included

Project template for Xamarin.Forms 5 Class Library and is named as formsclasslib

Class library project template take the below optional parameters to override its target framework and to include the officially supported Xamarin.CommunityToolkit, Xamarin.CommunityToolkit.Markup, Xamarin.Essentials, CommunityToolkit.Mvvm (aka Microsoft MVVM Toolkit), VijayAnand.Toolkit.Markup (the Shared Toolkit) or all NuGet packages:

  • -f | --framework - Default value is netstandard2.0
  • -asp | --all-supported-packages - Default value is false
  • -it | --include-toolkit - Default value is false
  • -im | --include-markup - Default value is false
  • -ie | --include-essentials - Default value is false
  • -imt | --include-mvvm-toolkit - Default value is false
  • -ist | --include-shared-toolkit - Default value is false

Explicit option:

  • -iei | --include-essentials-interfaces - Default value is false

Note: The NuGet package version being added out-of-the-box are v2.0.x for the Xamarin Toolkit packages, v1.8.x for the Xamarin.Essentials package, and v8.2.x for the MVVM Toolkit package.

Item Template Name
Xamarin.Forms Item (XAML) forms-item
Xamarin.Forms Item (C#) forms-item-cs
ContentPage and ViewModel (XAML) forms-mvvm
ContentPage and ViewModel (C#) forms-mvvm-cs
ContentPage (XAML) forms-page
ContentPage (C#) forms-page-cs
ContentView (XAML) forms-view
ContentView (C#) forms-view-cs
ResourceDictionary (XAML) forms-resdict
ShellPage (XAML) forms-shell
ShellPage (C#) forms-shell-cs

Use the below .NET CLI command to create the project, pages, and views out these templates:

Class Library:

dotnet new formsclasslib -o MyApp.Core

Library target framework override:

dotnet new formsclasslib -o MyApp.Core -f netstandard2.1

Option to include NuGet packages:

dotnet new formsclasslib -o MyApp.UI -it -im -ie -iei -imt -ist

In a single parameter (Essentials Interfaces is an exception, to be explicitly mentioned):

dotnet new formsclasslib -o MyApp.UI -asp -iei

NuGet Central Package Management (CPM) feature:

For now, this is supported only on CLI. Can be used in combination with other parameters too.

dotnet new formsclasslib -o MyApp.UI -cpm

Here, -n denotes the name of the Page/View that is to be created (for Pages/Shell/Views, don't need to suffix the type like HomePage, OrderView, AppShell and file extension .xaml / .cs, it will be added automatically) (Can also be specified as --name in the expanded form).

Just mention only the name of the item that is to be created. Item Type and File Extension would be automatically added.

Note: If name parameter is not provided, the .NET CLI template engine will take the current folder name in the context as its name.

And -na denotes the namespace under which the file is to be created (Can also be specified as --namespace in the expanded form).

While working with .NET 7 or higher SDK, the namespace parameter in short notation needs to be passed as -p:na (i.e., it needs to be prefixed with -p:).

Generic Item Templates:

Xamarin.Forms Generic Item Templates by Vijay Anand E G

  • A revolutionary generic item template, in XAML and C#, for creating items of any type
  • Supported both within the VS2022 IDE and CLI
  • On CLI, it is named as forms-item and forms-item-cs
  • The same set of parameters is defined in the UI as Dropdown, TextBox and CheckBox for ease of use
  • Both need one required parameter, -b / --base, the base type
  • Optionally takes another parameter, -g / --generic, to specify the generic base type
  • In addition, the XAML template takes one more parameter, -xo / --xaml-only, when defined, generates only the XAML definition
  • Frequently used base types are loaded in the Editable dropdown, user can also enter their value here
  • Ensure the values are entered in Pascal notation. XAML templates support XML namespace prefix, quite like how it is used in real world (xct:Popup)
  • The one big advantage of using this on IDE is the relative namespace to the folder where the item is created whereas on CLI, this defaults to the root namespace. As relative namespace resolution is yet to be fully supported by the CLI templating engine and is actively tracked here

Xamarin.Forms Generic Item Dialog by Vijay Anand E G

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 XAML template, pass the xmlns scope, like xct:Popup, as part of the input parameter value and it'll be used appropriately in the generated source files.

Tip: Tip: Use local scope to refer to the types in the same directory like Views. For e.g., local:BasePage.

Example:

dotnet new forms-item -n ThemePopup -b xct:Popup -p:na MyApp.Views

Output: ThemePopup.xaml and ThemePopup.xaml.cs

public partial class ThemePopup : Popup {}
dotnet new forms-item -n SearchPage -b FormsPage -g SearchViewModel -p:na MyApp.Views

Output: SearchPage.xaml and SearchPage.xaml.cs

public partial class SearchPage : FormsPage<SearchViewModel> {}

MVVM Item Templates:

  • Introduced an item template to generate Page and its ViewModel in a single command, available for both XAML and C#
  • The Page will be generated in the Views folder and ViewModel will be generated in the ViewModels folder
  • Can also be overridden to generate in the same folder with the -sf | --same-folder option
  • The ViewModels are generated with the base class titled BaseViewModel (implementation left to the user)
  • Including CommunityToolkit.Mvvm, an officially supported NuGet package, would be the best fit as it makes it easy to work with the MVVM design pattern
  • And it's recommended to use this MVVM template from the project root. So that output is aligned to the folder structure

Note: Don't suffix anything to the name, it'll be included automatically.

dotnet new forms-mvvm -n Login -p:na MyApp
dotnet new forms-mvvm-cs -n Search -p:na MyApp

Output Structure:

XAML:

ViewModels  LoginViewModel.cs Views  LoginPage.xaml  LoginPage.xaml.cs

C#:

ViewModels  SearchViewModel.cs Views  SearchPage.cs

Page:

dotnet new forms-page -n Login -na MyApp.Views
dotnet new forms-page-cs -n Home -na MyApp.Views

View:

dotnet new forms-view -n Card -na MyApp.Views
dotnet new forms-view-cs -n Order -na MyApp.Views

Shell:

dotnet new forms-shell -n App -na MyApp
dotnet new forms-shell-cs -n Mobile -na MyApp

Resource Dictionary:

With C# code-behind file:

dotnet new forms-resdict -n DefaultTheme -na MyApp.Themes

Without C# code-behind file - Xaml only (The option to be specified is -xo or --xaml-only):

dotnet new forms-resdict -n DarkTheme -na MyApp.Themes -xo