Aren't bindable properties in Xamarin a bit of a mess?

GaryMcGheeGaryMcGhee AUMember ✭✭
edited July 2015 in General

I'm new to Xamarin, and really excited about what it means for app development across platform yet native at the same time. I also have lots of property/binding experience in Delphi & EmberJS, and I'm not in the MS ecosystem, so I guess I have a different perspective from most of you.

Traditionally, the whole point of setters and getters is to encapsulate changes. The setter should be the only way to modify the given internal value:

"The mutator method is most often used in object-oriented programming, in keeping with the principle of encapsulation. According to this principle, member variables of a class are made private to hide and protect them from other code, and can only be modified by a public member function (the mutator method), which takes the desired new value as a parameter, optionally validates it, and modifies the private member variable."

Excuse me, but aren't bindable properties in Xamarin a bit of a mess?

See this issue another poor newbie had :

I've discovered the same issue when XAML sets properties. This means we have to provide a setter in order for code to modify the property nicely (without SetValue) yet the setter doesn't give us any benefit by encapsulating changes (we need OnPropertyChanged/OnPropertyChanging). Bindable properties therefore typically require 4 or 5 things :

1) BindableProperty.Create
2) getter
3) setter
4) OnPropertyChanging
5) OnPropertyChanged

Also the code required for a bindable property is a bit ridiculous

public class MyBoxView : BoxView {
    public static readonly BindableProperty DiameterProperty = BindableProperty.Create(p => p.Diameter, default(double),bindingPropertyChanged: OnDiameterPropertyChanged);

    public double Diameter {
        get { return (double)GetValue(DiameterProperty); }
        set { SetValue(DiameterProperty, value); }

    private static void OnDiameterPropertyChanged(BindableObject sender, double oldValue, double newValue) {
        MyBoxView thisControl = (MyBoxView)sender;

        thisControl.WidthRequest = newValue;
        thisControl.HeightRequest = newValue;

Checkout EmberJs for comparison (hypothetically) :

MyBoxView = BoxView.extend({ Diameter: 0, // bindable by default! Complex properties can provide getter & setter code and dependencies that trigger an update OnDiameterChanged: function() { // an observer this.set('WidthRequest',this.get('Diameter')); // yes, set is required everywhere, but won't be soon when JS has true setters and getters this.set('HeightRequest',this.get('Diameter')); }.observes('Diameter'); // can list many things to observe here, including dotted paths })

Suggestions :

1) Please improve this while Xamarin is still young
2) Make GetValue/SetValue protected, or at least discourage their use from outside the component
3) XAML should use the given setters and getters and fail if they don't exist

Sign In or Register to comment.