X.BindableProperty.Generator 0.3.1

dotnet add package X.BindableProperty.Generator --version 0.3.1                
NuGet\Install-Package X.BindableProperty.Generator -Version 0.3.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="X.BindableProperty.Generator" Version="0.3.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add X.BindableProperty.Generator --version 0.3.1                
#r "nuget: X.BindableProperty.Generator, 0.3.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install X.BindableProperty.Generator as a Cake Addin
#addin nuget:?package=X.BindableProperty.Generator&version=0.3.1

// Install X.BindableProperty.Generator as a Cake Tool
#tool nuget:?package=X.BindableProperty.Generator&version=0.3.1                

NuGet GitHub issues GitHub stars last commit

Xamarin.BindableProperty.Generator

Source generator that automatically transforms fields into BindableProperties that can be used in Xamarin.
Are you looking for the MAUI project? check this link

Installation

First, install NuGet. Then, install X.BindableProperty.Generator from the package manager console:

PM> Install-Package X.BindableProperty.Generator

Usage - Simple implementation

Just decorate field with the Bindable attribute.

    using Xamarin.BindableProperty.Generator.Core;

    public partial class CustomEntry : ContentView
    {
        [AutoBindable]
        private string _placeholder;
    }

the prevoius code will generate this:

    public partial class CustomEntry
    {
        public static readonly Xamarin.Forms.BindableProperty PlaceholderProperty =
                                    Xamarin.Forms.BindableProperty.Create(
                                                    nameof(Placeholder),
                                                    typeof(string),
                                                    typeof(CustomEntry),
                                                    default(string));

        public string Placeholder
        {
            get => (string)GetValue(PlaceholderProperty);
            set => SetValue(PlaceholderProperty, value);
        }
    }

Usage - Custom property name

Just decorate field with the Bindable attribute.

    using Xamarin.BindableProperty.Generator.Core;

    public partial class CustomEntry : ContentView
    {
        [AutoBindable(PropertyName = "Text")]
        private string _t;
    }

the prevoius code will generate this:

    public partial class CustomEntry
    {
        public static readonly Xamarin.Forms.BindableProperty TextProperty =
                                    Xamarin.Forms.BindableProperty.Create(
                                                    nameof(Text),
                                                    typeof(string),
                                                    typeof(CustomEntry),
                                                    default(string));

        public string Text
        {
            get => (string)GetValue(TextProperty);
            set => SetValue(TextProperty, value);
        }
    }

Usage - OnChanged method

Example 1 - No Parameters

Just decorate field with the Bindable attribute.

    using Xamarin.BindableProperty.Generator.Core;

    public partial class HeaderControl : ContentView
    {
        [AutoBindable(OnChanged = nameof(UpdateDisplayName))]
        private string _firstName;

        private void UpdateDisplayName()
        { 
            // Do stuff here
        }
    }

the prevoius code will generate this:

    public partial class HeaderControl
    {
        public static readonly Xamarin.Forms.BindableProperty FirstNameProperty =
                                    Xamarin.Forms.BindableProperty.Create(
                                                    nameof(FirstName),
                                                    typeof(string),
                                                    typeof(HeaderControl),
                                                    defaultValue: default(string),
                                                    propertyChanged: __UpdateDisplayName);

        public string FirstName
        {
            get => (string)GetValue(FirstNameProperty);
            set => SetValue(FirstNameProperty, value);
        }

        private static void __UpdateDisplayName(Xamarin.Forms.BindableObject bindable, object oldValue, object newValue)
        {
            var ctrl = (HeaderControl)bindable;
            ctrl.UpdateDisplayName();
        }
    }

Example 2 - One Parameter

Just decorate field with the Bindable attribute. The 'UpdateDisplayName' method must have only one parameter (must match the type of the field)

    using Xamarin.BindableProperty.Generator.Core;

    public partial class HeaderControl : ContentView
    {
        [AutoBindable(OnChanged = nameof(UpdateDisplayName))]
        private string _firstName;

        private void UpdateDisplayName(string newValue)
        { 
            // Do stuff here
        }
    }

the prevoius code will generate this:

    public partial class HeaderControl
    {
        public static readonly Xamarin.Forms.BindableProperty FirstNameProperty =
                                    Xamarin.Forms.BindableProperty.Create(
                                                    nameof(FirstName),
                                                    typeof(string),
                                                    typeof(HeaderControl),
                                                    defaultValue: default(string),
                                                    propertyChanged: __UpdateDisplayName);

        public string FirstName
        {
            get => (string)GetValue(FirstNameProperty);
            set => SetValue(FirstNameProperty, value);
        }

        private static void __UpdateDisplayName(Xamarin.Forms.BindableObject bindable, object oldValue, object newValue)
        {
            var ctrl = (HeaderControl)bindable;
            ctrl.UpdateDisplayName((string)newValue);
        }
    }

Example 3 - Two Parameters

Just decorate field with the Bindable attribute. The 'UpdateDisplayName' method must have two parameters (must match the type of the field)

    using Xamarin.BindableProperty.Generator.Core;

    public partial class HeaderControl : ContentView
    {
        [AutoBindable(OnChanged = nameof(UpdateDisplayName))]
        private string _firstName;

        private void UpdateDisplayName(string oldValue, string newValue)
        { 
            // Do stuff here
        }
    }

the prevoius code will generate this:

    public partial class HeaderControl
    {
        public static readonly Xamarin.Forms.BindableProperty FirstNameProperty =
                                    Xamarin.Forms.BindableProperty.Create(
                                                    nameof(FirstName),
                                                    typeof(string),
                                                    typeof(HeaderControl),
                                                    defaultValue: default(string),
                                                    propertyChanged: __UpdateDisplayName);

        public string FirstName
        {
            get => (string)GetValue(FirstNameProperty);
            set => SetValue(FirstNameProperty, value);
        }

        private static void __UpdateDisplayName(Xamarin.Forms.BindableObject bindable, object oldValue, object newValue)
        {
            var ctrl = (HeaderControl)bindable;
            ctrl.UpdateDisplayName((string)oldValue, (string)newValue);
        }
    }

Usage - Set default value

Example 1 - DateTime

Just decorate field with the Bindable attribute and add the "text/value" that you want to use as default value.

    using Xamarin.BindableProperty.Generator.Core;

    public partial class HeaderControl : ContentView
    {
        [AutoBindable(DefaultValue = "System.DateTime.Now")]
        private DateTime _birthDate;
    }

the prevoius code will generate this:

    public partial class HeaderControl
    {
        public static readonly Xamarin.Forms.BindableProperty BirthDateProperty =
                                    Xamarin.Forms.BindableProperty.Create(
                                                    nameof(BirthDate),
                                                    typeof(System.DateTime),
                                                    typeof(HeaderControl),
                                                    defaultValue: System.DateTime.Now);

        public System.DateTime BirthDate
        {
            get => (System.DateTime)GetValue(BirthDateProperty);
            set => SetValue(BirthDateProperty, value);
        }
    }

Example 2 - String

Just decorate field with the Bindable attribute and add the "text/value" that you want to use as default value.

    using Xamarin.BindableProperty.Generator.Core;

    public partial class HeaderControl : ContentView
    {
        [AutoBindable(DefaultValue = "USA")]
        private string _country;
    }

the prevoius code will generate this:

    public partial class HeaderControl
    {
        public static readonly Xamarin.Forms.BindableProperty CountryProperty =
                                    Xamarin.Forms.BindableProperty.Create(
                                                    nameof(Country),
                                                    typeof(string),
                                                    typeof(HeaderControl),
                                                    defaultValue: "USA");

        public string Country
        {
            get => (string)GetValue(CountryProperty);
            set => SetValue(CountryProperty, value);
        }
    }

Usage - Set default BindingMode

Just decorate field with the Bindable attribute and add the "BindingMode" that you want to use as default value.

    using Xamarin.BindableProperty.Generator.Core;

    public partial class HeaderControl : ContentView
    {
        [AutoBindable(DefaultBindingMode = nameof(BindingMode.TwoWay))]
        private string _firstName;
    }

the prevoius code will generate this:

    public partial class HeaderControl
    {
        public static readonly Xamarin.Forms.BindableProperty FirstNameProperty =
                                    Xamarin.Forms.BindableProperty.Create(
                                                    nameof(FirstName),
                                                    typeof(string),
                                                    typeof(HeaderControl),
                                                    defaultValue: default(string),
                                                    defaultBindingMode: Xamarin.Forms.BindingMode.TwoWay);

        public string FirstName
        {
            get => (string)GetValue(FirstNameProperty);
            set => SetValue(FirstNameProperty, value);
        }
    }

Usage - Hide existing BindableProperties

Just decorate field with the Bindable attribute and set "HidesUnderlyingProperty = true".

    using Xamarin.BindableProperty.Generator.Core;

    public partial class HeaderControl : ContentView
    {
        [AutoBindable(HidesUnderlyingProperty = true)]
        private readonly Color _backgroundColor;
    }

the prevoius code will generate this:

    public partial class HeaderControl
    {
        public static new readonly Xamarin.Forms.BindableProperty BackgroundColorProperty =
                                        Xamarin.Forms.BindableProperty.Create(
                                                        nameof(BackgroundColor),
                                                        typeof(Xamarin.Forms.Color),
                                                        typeof(HeaderControl));

        public new Xamarin.Forms.Color BackgroundColor
        {
            get => (Xamarin.Forms.Color)GetValue(BackgroundColorProperty);
            set => SetValue(BackgroundColorProperty, value);
        }
    }

Project status

  • ✅ Simple implementation - Done
  • ✅ Custom property name - Done
  • ✅ Custom Parameters - Done
  • ✅ OnChanged method - Done
  • ✅ OnChanged method overloading - Done

Extra info

This repo is using part of the code of CodeWriter to generate the CSharp files, thanks to the author.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.3.1 2,681 7/27/2022
0.3.0 411 7/16/2022

Added "ExcludeFromCodeCoverage" attribute to generated code