NittyGritty.SourceGenerators
1.0.0
See the version list below for details.
dotnet add package NittyGritty.SourceGenerators --version 1.0.0
NuGet\Install-Package NittyGritty.SourceGenerators -Version 1.0.0
<PackageReference Include="NittyGritty.SourceGenerators" Version="1.0.0" />
paket add NittyGritty.SourceGenerators --version 1.0.0
#r "nuget: NittyGritty.SourceGenerators, 1.0.0"
// Install NittyGritty.SourceGenerators as a Cake Addin #addin nuget:?package=NittyGritty.SourceGenerators&version=1.0.0 // Install NittyGritty.SourceGenerators as a Cake Tool #tool nuget:?package=NittyGritty.SourceGenerators&version=1.0.0
Nitty Gritty Source Generators
This package includes the following source generators:
Property Generator
Generate observable properties by annotating your fields inside a class with [Notify]
. The class should have ObservableObject
as its base class. You can also annotate your field with [AlsoNotify]
to also raise the property changed event for other properties whenever the generated property is set.
You can customize the generated property by using the following properties of NotifyAttribute
:
- Name (Change the name of the generated property)
- Getter (Change the accessibility level of the property's getter)
- Setter (Change the accessibility level of the property's setter)
Example:
using NittyGritty;
using NittyGritty.SourceGenerators.Attributes;
public partial class Student : ObservableObject
{
[Notify]
[AlsoNotify(Name = "FullName")]
public string _firstName;
[Notify(Name = "LastName")]
[AlsoNotify(Name = "FullName")]
public string _surname;
public string FullName => $"{FirstName} {LastName}";
}
This will generate:
public partial class Student
{
public string FirstName
{
get { return _firstName; }
set
{
Set(ref _firstName, value);
RaisePropertyChanged("FullName");
}
}
public string LastName
{
get { return _surname; }
set
{
Set(ref _surname, value);
RaisePropertyChanged("FullName");
}
}
}
Command Generator
Generate commands by annotating your methods with [Command]
. The generator will rely on the method signature to generate the command:
- Return type is
void
⇒RelayCommand
- Return type is
Task
⇒AsyncRelayCommand
- Other return types ⇒ not supported
- No parameters ⇒ Non-generic Command
- One parameter ⇒ Generic Command with the parameter type as the type argument
- Two parameters or more ⇒ not supported
You can customize the generated command by using the following property of CommandAttribute
:
- Name (Change the name of the generated command)
Example:
using System.Threading.Tasks;
using NittyGritty.ViewModels;
using NittyGritty.SourceGenerators.Attributes;
public partial class HomeViewModel : ViewModelBase
{
[Command]
public void Load()
{
// ...
}
[Command]
public async Task LoadAsync()
{
// ...
}
}
This will generate:
using NittyGritty.Commands;
public partial class HomeViewModel
{
private RelayCommand _Load;
public RelayCommand LoadCommand => _Load ?? (_Load = new RelayCommand(
() =>
{
Load();
}
));
private AsyncRelayCommand _LoadAsync;
public AsyncRelayCommand LoadAsyncCommand => _LoadAsync ?? (_LoadAsync = new AsyncRelayCommand(
async () =>
{
await LoadAsync();
}
));
}
ViewModelKeys Generator
Generate a ViewModelKeys
class for all of your [ViewModelKey]
annotated view models to use for NavigationService
and more.
You can customize the generated view model key by using the following property of ViewModelKeyAttribute
:
- Key (Change the key of the generated view model key)
You can also customize the namespace of the generated ViewModelKeys
class by using the MSBuild property NG_ViewModelKeysNamespace
in your .csproj file.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<NG_ViewModelKeysNamespace>NittyGritty.Sample</NG_ViewModelKeysNamespace>
</PropertyGroup>
</Project>
Example:
using NittyGritty.ViewModels;
using NittyGritty.SourceGenerators.Attributes;
[ViewModelKey]
public class HomeViewModel : ViewModelBase { /* ... */ }
[ViewModelKey(Key = "NormalName")]
public class WeirdNameViewModel : ViewModelBase { /* ... */ }
This will generate:
namespace NittyGritty.Generated
{
public static class ViewModelKeys
{
public static string Home { get; } = nameof(Home);
public static string NormalName { get; } = nameof(NormalName);
}
}
DialogKeys Generator
Generate a DialogKeys
class for all of your [DialogKey]
annotated view models to use for DialogService
and more.
You can customize the generated dialog key by using the following property of DialogKeyAttribute
:
- Key (Change the key of the generated dialog key)
You can also customize the namespace of the generated DialogKeys
class by using the MSBuild property NG_DialogKeysNamespace
in your .csproj file.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<NG_DialogKeysNamespace>NittyGritty.Sample</NG_DialogKeysNamespace>
</PropertyGroup>
</Project>
Example:
using NittyGritty;
using NittyGritty.SourceGenerators.Attributes;
[DialogKey]
public class ConfirmDeleteViewModel : ObservableObject { /* ... */ }
[DialogKey(Key = "NormalName")]
public class WeirdNameViewModel : ObservableObject { /* ... */ }
This will generate:
namespace NittyGritty.Generated
{
public static class DialogKeys
{
public static string ConfirmDelete { get; } = nameof(ConfirmDelete);
public static string NormalName { get; } = nameof(NormalName);
}
}
NOTES
Don't forget to make your classes partial when using the [Notify]
and [Command]
attributes. If the generated source code is not being updated even after doing a clean and rebuild, try restarting Visual Studio.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- NittyGritty (>= 2.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.