Skip to content

Commit

Permalink
Added support to hide existing BindableProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
rrmanzano committed Apr 28, 2022
1 parent 7f9b96a commit 299f94f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,32 @@ the prevoius code will generate this:
}
```

## Usage - Hide existing BindableProperties
Just decorate field with the Bindable attribute and set "HidesUnderlyingProperty = true".

```csharp
using Maui.BindableProperty.Generator.Core;

public partial class HeaderControl : ContentView
{
[AutoBindable(HidesUnderlyingProperty = true)]
private readonly Color _backgroundColor;
}
```
the prevoius code will generate this:
```csharp
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);
}
}
```

## Project status

- ✅ Simple implementation - Done
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public partial class HeaderControl : ContentView
[AutoBindable]
private readonly string _displayName;

[AutoBindable(HidesUnderlyingProperty = true, DefaultValue = "Color.FromArgb(\"#bdbde6\")")]
private readonly Color _backgroundColor;

#pragma warning restore CS0169

public HeaderControl()
Expand All @@ -37,9 +40,9 @@ private void UpdateDisplayName()
name = FirstName;
}

if (!string.IsNullOrEmpty(LastName))
if (!string.IsNullOrEmpty(this.LastName))
{
name = $"{name} {LastName}";
name = $"{name} {this.LastName}";
}

this.DisplayName = name.Trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ public class AutoBindableConstants
public const string AttrDefaultValue = "DefaultValue";

public const string AttrDefaultBindingMode = "DefaultBindingMode";

public const string AttrHidesUnderlyingProperty = "HidesUnderlyingProperty";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public AutoBindableAttribute(){}
public string DefaultValue { get; set; }
public string DefaultBindingMode { get; set; }
public bool HidesUnderlyingProperty { get; set; } = false;
}
}";

Expand Down Expand Up @@ -84,10 +86,12 @@ private void ProcessBindableProperty(CodeWriter w, IFieldSymbol fieldSymbol, ISy

var bindablePropertyName = $@"{propertyName}Property";
var customParameters = this.ProcessBindableParameters();
var applyHidesUnderlying = fieldSymbol.GetValue<bool>(attributeSymbol, AutoBindableConstants.AttrHidesUnderlyingProperty);
var hidesUnderlying = applyHidesUnderlying ? " new" : string.Empty;
w._(AutoBindableConstants.AttrGeneratedCodeString);
w._($@"public static readonly {AutoBindableConstants.FullNameMauiControls}.BindableProperty {bindablePropertyName} = {AutoBindableConstants.FullNameMauiControls}.BindableProperty.Create(nameof({propertyName}), typeof({fieldType}), typeof({classSymbol.Name}){customParameters});");
w._($@"public static{hidesUnderlying} readonly {AutoBindableConstants.FullNameMauiControls}.BindableProperty {bindablePropertyName} = {AutoBindableConstants.FullNameMauiControls}.BindableProperty.Create(nameof({propertyName}), typeof({fieldType}), typeof({classSymbol.Name}){customParameters});");
w._(AutoBindableConstants.AttrGeneratedCodeString);
using (w.B(@$"public {fieldType} {propertyName}"))
using (w.B(@$"public{hidesUnderlying} {fieldType} {propertyName}"))
{
w._($@"get => ({fieldType})GetValue({bindablePropertyName});");
if (this.ExistsBodySetter())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ public static T GetValue<T>(
var value = TypedConstant.Value;
if (value.GetType() == typeof(T))
{
return onSuccess.Invoke((T)value);
if (onSuccess != null)
{
return onSuccess.Invoke((T)value);
}

return (T)value;
}
}

Expand Down

0 comments on commit 299f94f

Please sign in to comment.