dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.2k stars 1.75k forks source link

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

Open egvijayanand opened 3 months ago

egvijayanand commented 3 months ago

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 commented 3 months ago

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

egvijayanand commented 3 months ago

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 commented 3 months ago

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