Skip to content

chinwobble/Chinwobble.Roslyn

Repository files navigation

Chinwobble.Roslyn

Roslyn-powered build-time code generation to generate the boilerplate needed to wire up WPF DependencyProperty and Xamarin BindableProperty at build time, accessible from intellisense on save.

Simply, install the required packages add the [GenerateNotifyPropertyChanged] to your ViewModels and [GenerateDependencyProperty] to your views.

Usage

Mark your View / ViewModel as partial and add the appropriate attribute.

Views

[GenerateDependencyProperty]
public partial class ChartControl : System.Windows.Controls.Control
{
    public static readonly DependencyProperty YScalePrecisionProperty =
        DependencyProperty.Register(
            name: "YScalePrecision",
            propertyType: typeof(int),
            ownerType: typeof(ChartControl));
}

The compiler will automatically generate _wrapper_ properties for each BindableProperty and DependencyProperty on save.
```csharp
partial class ChartControl
{
    public int YScalePrecision
    {
        get
        {
            return (int)GetValue(YScalePrecisionProperty);
        }

        set
        {
            SetValue(YScalePrecisionProperty, value);
        }
    }
}

View Models

Mark your class as partial and add the attribute [GenerateNotifyPropertyChanged].

[GenerateNotifyPropertyChanged]
public partial class MainViewModel : BindableObject
{
    private string _title;
    private string _name;
}

This package will automatically generate this boilerplate for you.

partial class MainViewModel
{
    public string Title
    {
        get => _title;
        set
        {
            if (_title != value)
            {
                _title = value;
                OnPropertyChanged(nameof(Title));
            }
        }
    }

    public string Name
    {
        get => _name;
        set
        {
            if (_name != value)
            {
                _name = value;
                OnPropertyChanged(nameof(Name));
            }
        }
    }
}

Installing

To use the the StronglyTypedId NuGet package you must add three packages:

To install the packages, add the references to your csproj file so that it looks something like the following:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <!-- Add these packages-->
  <ItemGroup>
    <PackageReference Include="Chinwobble.Roslyn" Version="0.1.2" />
    <DotNetCliToolReference Include="dotnet-codegen" Version="0.5.13" />
  </ItemGroup>
  <!-- -->

</Project>

Restore the tools using dotnet restore.

Note this package and dotnet-codegen are build time dependencies - no extra dll's are added to your project's output! It's as though you wrote standard C# code yourself!

This package requires your project to anything compatible with .NET Standard 2.0 The code generation DotNetCliTool (dotnet-codegen) is only supported in SDK-format csproj projects

About

Roslyn powered generators to remove boilerplate

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages