Skip to content

Commit

Permalink
Included Shell C# item template
Browse files Browse the repository at this point in the history
  • Loading branch information
egvijayanand committed Mar 16, 2023
1 parent 25928eb commit 8c76224
Show file tree
Hide file tree
Showing 29 changed files with 388 additions and 107 deletions.
62 changes: 38 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Join me on [**Developer Thoughts**](https://egvijayanand.in/), an exclusive blog

|NuGet|VS Marketplace|
|:---:|:---:|
|[![VijayAnand.FormsTemplates - NuGet Package](https://badgen.net/nuget/v/VijayAnand.FormsTemplates/)](https://www.nuget.org/packages/VijayAnand.FormsTemplates/)|[![Xamarin.Forms Project and Item Templates - VS Marketplace](https://badgen.net/vs-marketplace/v/egvijayanand.xamarin-forms-templates)](https://marketplace.visualstudio.com/items?itemName=egvijayanand.xamarin-forms-templates)|
|[![VijayAnand.FormsTemplates - NuGet Package](https://badgen.net/nuget/v/VijayAnand.FormsTemplates/?icon=nuget)](https://www.nuget.org/packages/VijayAnand.FormsTemplates/)|[![Xamarin.Forms Project and Item Templates - VS Marketplace](https://badgen.net/vs-marketplace/v/egvijayanand.xamarin-forms-templates?icon=visualstudio)](https://marketplace.visualstudio.com/items?itemName=egvijayanand.xamarin-forms-templates)|

#### Access within Visual Studio IDE

Expand Down Expand Up @@ -42,32 +42,50 @@ dotnet new update --check-only
```shell
dotnet new update
```

#### Templates Included

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

And it currently targets Xamarin.Forms 5.0 Service Release 12 (ver. 5.0.0.2515).
Class library project template take the below optional parameters to override its target framework and to include the officially supported [Xamarin.CommunityToolkit](https://www.nuget.org/packages/Xamarin.CommunityToolkit), [Xamarin.CommunityToolkit.Markup](https://www.nuget.org/packages/Xamarin.CommunityToolkit.Markup), [Xamarin.Essentials](https://www.nuget.org/packages/Xamarin.Essentials), [CommunityToolkit.Mvvm](https://www.nuget.org/packages/CommunityToolkit.Mvvm) (aka Microsoft MVVM Toolkit) or all NuGet packages:

* `-f` | `--framework` - Default value is `netstandard2.0`
* `-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`

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

Item templates for `ContentPage`, `ContentView`, `ShellPage`, and `ResourceDictionary` in XAML and named as `forms-page`, `forms-view`, `forms-shell`, and `forms-resdict` respectively. There's a C# item template for `ContentPage` and `ContentView` and named as `forms-page-cs` and `forms-view-cs` respectively.
|Item|Template Name|
|:---:|:---:|
|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:

An optional parameter has been introduced to choose the library target framework (.NET Standard 2.0 / 2.1):
Here, `-n` denotes the name of the project/page/view that is to be created (for pages/views, don't need to suffix it with `.xaml` / `.cs`, it will be added automatically) (Can also be specified as `--name` in the expanded form).

* `-f` | `--framework` - Default value is `netstandard2.0`
*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:`).*

Class Library:
```shell
dotnet new formsclasslib -n MyApp.Core
```
Library target framework override:
```shell
dotnet new formsclasslib -n MyApp.Core -f netstandard2.1
```

Class library project templates take the below optional parameters to include the officially supported `Xamarin.CommunityToolkit`, `Xamarin.CommunityToolkit.Markup`, `Xamarin.Essentials`, `CommunityToolkit.Mvvm` (aka Microsoft MVVM Toolkit) or all NuGet packages:

* `-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`

Note: `v2.0.x` is the NuGet package version being added out-of-the-box for Xamarin Toolkit packages, `v1.7.3` for Xamarin.Essentials package and `v8.0.0` for MVVM Toolkit package.

Option to include NuGet packages:
```shell
dotnet new formsclasslib -n MyApp.UI -it -im -ie -imt
```
Expand All @@ -92,21 +110,17 @@ Shell:
```shell
dotnet new forms-shell -n AppShell -na MyApp
```
```shell
dotnet new forms-shell-cs -n MobileShell -na MyApp
```

Resource Dictionary:

With C# code-behind file:
```shell
dotnet new forms-resdict -n DarkTheme -na MyApp.Themes
dotnet new forms-resdict -n DefaultTheme -na MyApp.Themes
```

Without C# code-behind file - Xaml Only (Option can be `-xo` or `--xaml-only`):
Without C# code-behind file - Xaml only (Option can be specified as `-xo` or `--xaml-only`):
```shell
dotnet new forms-resdict -n DarkTheme -na MyApp.Themes -xo
```

Here `-n` denotes the name of the project/page/view that is to be created (for pages/views, don't need to suffix it with .xaml, it will be added automatically) (Can also be specified as `--name`).

*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`). _While working with .NET 7 SDK, the `namespace` parameter in short notation needs to be passed as `-p:na` (i.e., it needs to be prefixed with `-p:`)._
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"Library"
],
"identity": "VijayAnand.FormsTemplates.ClassLib",
"description": "A project template for Xamarin.Forms 5 Class Library (ver. 5.0.0.2515)",
"groupIdentity": "VijayAnand.FormsTemplates.Library",
"description": "A project template for Xamarin.Forms 5 Class Library",
"name": "Xamarin.Forms 5 Class Library",
"shortName": "formsclasslib",
"sourceName": "FormsClassLib.1",
Expand Down
13 changes: 7 additions & 6 deletions src/FormsTemplatesCLI/FormsClassLib/FormsClassLib.1.csproj
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>LIB_TFM</TargetFramework>
<!-- Project Options -->
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>FormsClassLib._1</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2515"/>
<PackageReference Include="Xamarin.Forms" Version="5.0.0.*"/>
<!--#if (AddToolkitPackage)-->
<PackageReference Include="Xamarin.CommunityToolkit" Version="2.0.5" />
<PackageReference Include="Xamarin.CommunityToolkit" Version="2.0.*" />
<!--#endif-->
<!--#if (AddMarkupPackage)-->
<PackageReference Include="Xamarin.CommunityToolkit.Markup" Version="2.0.5" />
<PackageReference Include="Xamarin.CommunityToolkit.Markup" Version="2.0.*" />
<!--#endif-->
<!--#if (AddEssentialsPackage)-->
<PackageReference Include="Xamarin.Essentials" Version="1.7.3" />
<PackageReference Include="Xamarin.Essentials" Version="1.7.*" />
<!--#endif-->
<!--#if (AddMvvmToolkitPackage)-->
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.0.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.1.0" />
<!--#endif-->
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"$schema": "http://json.schemastore.org/vs-2017.3.host"
}
}
28 changes: 26 additions & 2 deletions src/FormsTemplatesCLI/FormsPage/.template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"UWP",
"Windows",
"Xamarin.Forms",
"Page",
"Xaml"
],
"identity": "VijayAnand.FormsPage",
Expand All @@ -21,7 +22,8 @@
"path": "FormsPage.1.xaml"
},
{
"path": "FormsPage.1.xaml.cs"
"path": "FormsPage.1.xaml.cs",
"condition": "(!XamlOnly)"
}
],
"tags": {
Expand All @@ -34,6 +36,28 @@
"replaces": "MyApp.Namespace",
"datatype": "text",
"description": "namespace for the generated code"
},
"xaml-only": {
"type": "parameter",
"datatype": "bool",
"default": "false",
"description": "If true, the output is generated without a C# code-behind file."
},
"XamlOnly": {
"type": "computed",
"value": "(xaml-only)"
}
},
"sources": [
{
"modifiers": [
{
"condition": "(XamlOnly)",
"exclude": [
"FormsPage.1.xaml.cs"
]
}
]
}
}
]
}
34 changes: 25 additions & 9 deletions src/FormsTemplatesCLI/FormsPage/FormsPage.1.xaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyApp.Namespace"
x:Class="MyApp.Namespace.FormsPage__1">
<!--#if (XamlOnly)-->
<?xaml-comp compile="true" ?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:local="clr-namespace:MyApp.Namespace"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<!--#else-->
<ContentPage
x:Class="MyApp.Namespace.FormsPage__1"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:local="clr-namespace:MyApp.Namespace"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<!--#endif-->
<ContentPage.Content>
<StackLayout>
<Label Text="Welcome to Xamarin.Forms!!!"
TextColor="Purple"
HorizontalOptions="Center"
VerticalOptions="Center"/>
<Label
HorizontalOptions="Center"
Text="Welcome to Xamarin.Forms!!!"
TextColor="Purple"
VerticalOptions="Center"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"$schema": "http://json.schemastore.org/vs-2017.3.host"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"UWP",
"Windows",
"Xamarin.Forms",
"Page",
"Code"
],
"identity": "VijayAnand.FormsPageCS",
Expand Down
1 change: 1 addition & 0 deletions src/FormsTemplatesCLI/FormsPageCS/FormsPage.1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public FormsPage__1()
new Label()
{
Text = "Hello from Xamarin.Forms!!!",
TextColor = Color.Purple,
HorizontalOptions = LayoutOptions.CenterAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"$schema": "http://json.schemastore.org/vs-2017.3.host"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
"name": "Xamarin.Forms ResourceDictionary",
"shortName": "forms-resdict",
"sourceName": "FormsResDict.1",
"primaryOutputs": [
{
"path": "FormsResDict.1.xaml"
},
{
"path": "FormsResDict.1.xaml.cs",
"condition": "(!XamlOnly)"
}
],
"tags": {
"language": "C#",
"type": "item"
Expand Down
22 changes: 15 additions & 7 deletions src/FormsTemplatesCLI/FormsResDict/FormsResDict.1.xaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--#if (XamlOnly)-->
<?xaml-comp compile="true" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyApp.Namespace">
<ResourceDictionary
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:local="clr-namespace:MyApp.Namespace"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<!-- Define your resources here -->
</ResourceDictionary>
<!--#else-->
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyApp.Namespace"
x:Class="MyApp.Namespace.FormsResDict__1">
<ResourceDictionary
x:Class="MyApp.Namespace.FormsResDict__1"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:local="clr-namespace:MyApp.Namespace"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<!-- Define your resources here -->
</ResourceDictionary>
<!--#endif-->
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"$schema": "http://json.schemastore.org/vs-2017.3.host"
}
}
30 changes: 27 additions & 3 deletions src/FormsTemplatesCLI/FormsShell/.template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
"UWP",
"Windows",
"Xamarin.Forms",
"Shell",
"Xaml"
],
"identity": "VijayAnand.FormsShell",
"groupIdentity": "VijayAnand.FormsTemplates.Shell",
"groupIdentity": "VijayAnand.FormsTemplates.Shell.Xaml",
"description": "An item template for Xamarin.Forms ShellPage in XAML",
"name": "Xamarin.Forms ShellPage",
"shortName": "forms-shell",
Expand All @@ -21,7 +22,8 @@
"path": "FormsShell.1.xaml"
},
{
"path": "FormsShell.1.xaml.cs"
"path": "FormsShell.1.xaml.cs",
"condition": "(!XamlOnly)"
}
],
"tags": {
Expand All @@ -34,6 +36,28 @@
"replaces": "MyApp.Namespace",
"datatype": "text",
"description": "namespace for the generated code"
},
"xaml-only": {
"type": "parameter",
"datatype": "bool",
"default": "false",
"description": "If true, the output is generated without a C# code-behind file."
},
"XamlOnly": {
"type": "computed",
"value": "(xaml-only)"
}
},
"sources": [
{
"modifiers": [
{
"condition": "(XamlOnly)",
"exclude": [
"FormsShell.1.xaml.cs"
]
}
]
}
}
]
}
Loading

0 comments on commit 8c76224

Please sign in to comment.