M.BindableProperty.Generator
0.9.1
See the version list below for details.
dotnet add package M.BindableProperty.Generator --version 0.9.1
NuGet\Install-Package M.BindableProperty.Generator -Version 0.9.1
<PackageReference Include="M.BindableProperty.Generator" Version="0.9.1" />
paket add M.BindableProperty.Generator --version 0.9.1
#r "nuget: M.BindableProperty.Generator, 0.9.1"
// Install M.BindableProperty.Generator as a Cake Addin #addin nuget:?package=M.BindableProperty.Generator&version=0.9.1 // Install M.BindableProperty.Generator as a Cake Tool #tool nuget:?package=M.BindableProperty.Generator&version=0.9.1
Maui.BindableProperty.Generator
Source generator that automatically transforms fields into BindableProperties that can be used in MAUI.
Are you looking for the Xamarin project? check this link
Installation
First, install NuGet. Then, install M.BindableProperty.Generator from the package manager console:
PM> Install-Package M.BindableProperty.Generator
Usage - Simple implementation
Just decorate the field with the Bindable attribute.
using Maui.BindableProperty.Generator.Core;
public partial class CustomEntry : ContentView
{
[AutoBindable]
private readonly string _placeholder;
}
the prevoius code will generate this:
public partial class CustomEntry
{
public static readonly Microsoft.Maui.Controls.BindableProperty PlaceholderProperty =
Microsoft.Maui.Controls.BindableProperty.Create(
nameof(Placeholder),
typeof(string),
typeof(CustomEntry),
default(string));
public string Placeholder
{
get => (string)GetValue(PlaceholderProperty);
set => SetValue(PlaceholderProperty, value);
}
}
Options
Option | Description | Type |
---|---|---|
PropertyName |
Specifies the prefix used to create the BindableProperty, if PropertyName is null, then will take the name of the field removing the underscores and using PascalCase. Example: _tex_t will be transformed to Text . |
string |
OnChanged |
Specifies the name of the method to be executed when the property changed. | string |
DefaultValue |
Specifies the "text/value" that will be used as default. Example: default(string) |
string |
DefaultBindingMode |
Specifies the "BindingMode" as string that will be used as default. Options: Default/TwoWay/OneWay/OneWayToSource/OneTime . Example: nameof(BindingMode.TwoWay) |
string |
HidesUnderlyingProperty |
Specifies if the BindingProperty will hide the current implementation. | bool |
ValidateValue |
Specifies the name of the method to be executed to validate the values. | string |
Usage - PropertyName
Just decorate the field with the Bindable attribute.
using Maui.BindableProperty.Generator.Core;
public partial class CustomEntry : ContentView
{
[AutoBindable(PropertyName = "Text")]
private readonly string _t;
}
the prevoius code will generate this:
public partial class CustomEntry
{
public static readonly Microsoft.Maui.Controls.BindableProperty TextProperty =
Microsoft.Maui.Controls.BindableProperty.Create(
nameof(Text),
typeof(string),
typeof(CustomEntry),
default(string));
public string Text
{
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
}
Usage - OnChanged
Example 1 - No Parameters
Just decorate the field with the Bindable attribute.
using Maui.BindableProperty.Generator.Core;
public partial class HeaderControl : ContentView
{
[AutoBindable(OnChanged = nameof(UpdateDisplayName))]
private readonly string _firstName;
private void UpdateDisplayName()
{
// Do stuff here
}
}
the prevoius code will generate this:
public partial class HeaderControl
{
public static readonly Microsoft.Maui.Controls.BindableProperty FirstNameProperty =
Microsoft.Maui.Controls.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(Microsoft.Maui.Controls.BindableObject bindable, object oldValue, object newValue)
{
var ctrl = (HeaderControl)bindable;
ctrl.UpdateDisplayName();
}
}
Example 2 - One Parameter
Just decorate the field with the Bindable attribute. The 'UpdateDisplayName' method must have only one parameter (must match the type of the field)
using Maui.BindableProperty.Generator.Core;
public partial class HeaderControl : ContentView
{
[AutoBindable(OnChanged = nameof(UpdateDisplayName))]
private readonly string _firstName;
private void UpdateDisplayName(string newValue)
{
// Do stuff here
}
}
the prevoius code will generate this:
public partial class HeaderControl
{
public static readonly Microsoft.Maui.Controls.BindableProperty FirstNameProperty =
Microsoft.Maui.Controls.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(Microsoft.Maui.Controls.BindableObject bindable, object oldValue, object newValue)
{
var ctrl = (HeaderControl)bindable;
ctrl.UpdateDisplayName((string)newValue);
}
}
Example 3 - Two Parameters
Just decorate the field with the Bindable attribute. The 'UpdateDisplayName' method must have two parameters (must match the type of the field)
using Maui.BindableProperty.Generator.Core;
public partial class HeaderControl : ContentView
{
[AutoBindable(OnChanged = nameof(UpdateDisplayName))]
private readonly 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 Microsoft.Maui.Controls.BindableProperty FirstNameProperty =
Microsoft.Maui.Controls.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(Microsoft.Maui.Controls.BindableObject bindable, object oldValue, object newValue)
{
var ctrl = (HeaderControl)bindable;
ctrl.UpdateDisplayName((string)oldValue, (string)newValue);
}
}
Usage - DefaultValue
Example 1 - DateTime
Just decorate the field with the Bindable attribute and add the "text/value" that you want to use as default value.
using Maui.BindableProperty.Generator.Core;
public partial class HeaderControl : ContentView
{
[AutoBindable(DefaultValue = "DateTime.Now")]
private readonly DateTime _birthDate;
}
the prevoius code will generate this:
public partial class HeaderControl
{
public static readonly Microsoft.Maui.Controls.BindableProperty BirthDateProperty =
Microsoft.Maui.Controls.BindableProperty.Create(
nameof(BirthDate),
typeof(System.DateTime),
typeof(HeaderControl),
defaultValue: DateTime.Now);
public System.DateTime BirthDate
{
get => (System.DateTime)GetValue(BirthDateProperty);
set => SetValue(BirthDateProperty, value);
}
}
Example 2 - String
Just decorate the field with the Bindable attribute and add the "text/value" that you want to use as default value.
using Maui.BindableProperty.Generator.Core;
public partial class HeaderControl : ContentView
{
[AutoBindable(DefaultValue = "USA")]
private readonly string _country;
}
the prevoius code will generate this:
public partial class HeaderControl
{
public static readonly Microsoft.Maui.Controls.BindableProperty CountryProperty =
Microsoft.Maui.Controls.BindableProperty.Create(
nameof(Country),
typeof(string),
typeof(HeaderControl),
defaultValue: "USA");
public string Country
{
get => (string)GetValue(CountryProperty);
set => SetValue(CountryProperty, value);
}
}
Usage - DefaultBindingMode
Just decorate the field with the Bindable attribute and add the "BindingMode" that you want to use as default value.
using Maui.BindableProperty.Generator.Core;
public partial class HeaderControl : ContentView
{
[AutoBindable(DefaultBindingMode = nameof(BindingMode.TwoWay))]
private readonly string _firstName;
}
the prevoius code will generate this:
public partial class HeaderControl
{
public static readonly Microsoft.Maui.Controls.BindableProperty FirstNameProperty =
Microsoft.Maui.Controls.BindableProperty.Create(
nameof(FirstName),
typeof(string),
typeof(HeaderControl),
defaultValue: default(string),
defaultBindingMode: Microsoft.Maui.Controls.BindingMode.TwoWay);
public string FirstName
{
get => (string)GetValue(FirstNameProperty);
set => SetValue(FirstNameProperty, value);
}
}
Usage - HidesUnderlyingProperty
Just decorate the field with the Bindable attribute and set "HidesUnderlyingProperty = true".
using Maui.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 Microsoft.Maui.Controls.BindableProperty BackgroundColorProperty =
Microsoft.Maui.Controls.BindableProperty.Create(
nameof(BackgroundColor),
typeof(Microsoft.Maui.Graphics.Color),
typeof(HeaderControl));
public new Microsoft.Maui.Graphics.Color BackgroundColor
{
get => (Microsoft.Maui.Graphics.Color)GetValue(BackgroundColorProperty);
set => SetValue(BackgroundColorProperty, value);
}
}
Usage - ValidateValue
Example 1 - Non-Static Method
Just decorate the field with the Bindable attribute. The 'ValidateNotNull' method must have two parameters (one of type BindableObject and the second one must be the same as the field)
using Maui.BindableProperty.Generator.Core;
public partial class HeaderControl : ContentView
{
[AutoBindable(ValidateValue = nameof(ValidateIsNullOrEmpty))]
private readonly string? _country;
private bool ValidateIsNullOrEmpty(BindableObject _, string? value)
{
// Do stuff here
return !string.IsNullOrEmpty(value);
}
}
the prevoius code will generate this:
public partial class HeaderControl
{
public static readonly Microsoft.Maui.Controls.BindableProperty CountryProperty =
Microsoft.Maui.Controls.BindableProperty.Create(
nameof(Country),
typeof(string),
typeof(HeaderControl),
validateValue: __ValidateIsNullOrEmpty);
public string? Country
{
get => (string?)GetValue(CountryProperty);
set => SetValue(CountryProperty, value);
}
private static bool __ValidateIsNullOrEmpty(Microsoft.Maui.Controls.BindableObject bindable, object value)
{
var ctrl = (HeaderControl)bindable;
return ctrl.ValidateIsNullOrEmpty(ctrl, (string?)value);
}
}
Example 2 - Static Method
Just decorate the field with the Bindable attribute. The 'ValidateNotNull' method must have two parameters (one of type BindableObject and the second one must be object type)
using Maui.BindableProperty.Generator.Core;
public partial class HeaderControl : ContentView
{
[AutoBindable(ValidateValue = nameof(ValidateNotNull))]
private readonly string? _country;
private static bool ValidateNotNull(BindableObject _, object value) => value != null;
}
the prevoius code will generate this:
public partial class HeaderControl
{
public static readonly Microsoft.Maui.Controls.BindableProperty CountryProperty =
Microsoft.Maui.Controls.BindableProperty.Create(
nameof(Country),
typeof(string),
typeof(HeaderControl),
defaultValue: default(string?),
validateValue: ValidateNotNull);
public string? Country
{
get => (string?)GetValue(CountryProperty);
set => SetValue(CountryProperty, value);
}
}
Do you want to remove the compiler warning CS0169 ?
using Maui.BindableProperty.Generator.Core;
public partial class HeaderControl : ContentView
{
#pragma warning disable CS0169
[AutoBindable]
private readonly string? _country;
#pragma warning restore CS0169
}
Project status
- ✅
PropertyName
- Done - ✅
OnChanged
- Done - ✅
DefaultValue
- Done - ✅
DefaultBindingMode
- Done - ✅
HidesUnderlyingProperty
- Done - ✅
ValidateValue
- Done
Extra info
This repo is using part of the code of CodeWriter to generate the CSharp files, thanks to the author.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages (6)
Showing the top 5 NuGet packages that depend on M.BindableProperty.Generator:
Package | Downloads |
---|---|
The49.Maui.ContextMenu
.NET MAUI library for Android and iOS to open a native context menu on long press. |
|
Sm.Maui.BottomSheet
ModalBottomSheet view for .NET MAUI |
|
SimpleRatingControl.MAUI
Simple Rating Control for .NET MAUI |
|
Yang.Maui.Helper.Controls
Package Description |
|
tsjdevapps.MauiControlsLib
Controls Library for .NET MAUI |
GitHub repositories (3)
Showing the top 3 popular GitHub repositories that depend on M.BindableProperty.Generator:
Repository | Stars |
---|---|
VladislavAntonyuk/MauiSamples
.NET MAUI Samples
|
|
the49ltd/The49.Maui.BottomSheet
.NET MAUI library used to display pages as Bottom Sheets
|
|
nor0x/Dots
the 🙂 friendly .NET SDK manager
|
Version | Downloads | Last updated |
---|---|---|
0.11.1 | 60,271 | 6/28/2023 |
0.11.0 | 203 | 6/28/2023 |
0.10.0 | 5,926 | 3/28/2023 |
0.9.3 | 8,856 | 12/7/2022 |
0.9.2 | 5,825 | 11/15/2022 |
0.9.1 | 913 | 9/27/2022 |
0.9.0 | 1,245 | 8/30/2022 |
0.8.3 | 1,341 | 7/27/2022 |
0.8.2 | 4,019 | 7/4/2022 |
0.8.1 | 550 | 7/1/2022 |
0.8.0 | 546 | 6/29/2022 |
0.7.0 | 657 | 4/28/2022 |
0.6.0 | 530 | 4/28/2022 |
0.5.0 | 517 | 4/21/2022 |
0.4.1 | 531 | 4/14/2022 |
0.4.0 | 535 | 4/12/2022 |
0.3.3 | 508 | 4/1/2022 |
0.3.2 | 501 | 4/1/2022 |
0.3.0 | 597 | 4/1/2022 |
Bugfix in defaultValue when "string.Empty"