maui icon indicating copy to clipboard operation
maui copied to clipboard

Make Typed `SetBinding()` method generic so that the `BindableProperty` need not be prefixed with the type name

Open egvijayanand opened this issue 1 year ago • 3 comments

Description

Make the Typed SetBinding() method generic so that the BindableProperty need not be prefixed with the type name.

By doing so, all the bindable properties of the object that invokes this method will be accessible.

Public API Changes

public static void SetBinding<TBindableObject, TSource, TProperty>(this TBindableObject bindable, BindableProperty targetProperty, Func<TSource, TProperty> getter /* all other parameters */) where TBindableObject : BindableObject {}

Intended Use-Case

var lbl = new Label();
// Current scenario:
lbl.SetBinding(Label.TextProperty, static (HomeViewModel vm) => vm.FullName);

// Updated scenario
// TextProperty is now accessible in the scope
lbl.SetBinding(TextProperty, static (HomeViewModel vm) => vm.FullName);

egvijayanand avatar Jul 12 '24 22:07 egvijayanand

When using generics, the method call becomes associated with the invocation object instance rather than the base BindableObject type.

egvijayanand avatar Jul 12 '24 22:07 egvijayanand

Is there a way for this method to return the updated instance of the BindableObject itself rather than returning void?

Is there any impediment to this? Enabling this would facilitate method call chaining.

egvijayanand avatar Jul 12 '24 22:07 egvijayanand

What is so special about bindable properties of the VisualElement type as I can refer to those properties without the type name prefix?

egvijayanand avatar Jul 12 '24 23:07 egvijayanand