Implement automatically BindableProperty
in Xamarin.Forms.
See also Fody usage.
Install the Kasay.BindableProperty.Fody NuGet package:
PM> Install-Package Kasay.BindableProperty.Fody -Version 1.0.3
** it's generated automatically after build.
Add <Kasay.BindableProperty/>
to FodyWeavers.xml
<?xml version="1.0" encoding="utf-8"?>
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<Kasay.BindableProperty />
</Weavers>
Before code:
public class DemoControl : UserControl
{
[Bind] public String SomeName { get; set; }
[Bind] public Int32 SomeNumber { get; set; }
[Bind] public Boolean SomeCondition { get; set; }
}
What gets compiled:
public class DemoControl : UserControl
{
public static readonly BindableProperty SomeNameProperty
= BindableProperty.Create(
"SomeName",
typeof(String),
typeof(DemoControl));
public String SomeName
{
get => (String)GetValue(SomeNameProperty);
set => SetValue(SomeNameProperty, value);
}
public static readonly BindableProperty SomeNumberProperty
= BindableProperty.Create(
"SomeName",
typeof(Int32),
typeof(DemoControl));
public Int32 SomeNumber
{
get => (Int32)GetValue(SomeNumberProperty);
set => SetValue(SomeNumberProperty, value);
}
public static readonly BindableProperty SomeConditionProperty
= BindableProperty.Create(
"SomeCondition",
typeof(Boolean),
typeof(DemoControl));
public Boolean SomeCondition
{
get => (Boolean)GetValue(SomeConditionProperty);
set => SetValue(SomeConditionProperty, value);
}
public DemoControl()
{
DataContext = this;
}
}
The implementation of BindableProperty in the generation of custom controls in Xamarin.Forms always leaves us with reduntant and repetitive code, but all this can be avoided using the Bind attribute of Kasay.BindableProperty.Fody.