You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
egvijayanand opened this issue
Dec 4, 2024
· 5 comments
Assignees
Labels
approvedchampionA member of the .NET MAUI Toolkit core team has chosen to champion this featureproposalA fully fleshed out proposal describing a new feature in syntactic and semantic detail
The Bind method behaves differently in the Typed Bindings approach compared to the classic String-based one.
In the classic approach, it is enough to specify the property to be bound. If the binding property's mode is two-way, the back update to the ViewModel property will happen automatically (similar to that of an XAML-based definition).
In the improved Typed Bindings approach, getters and setters are separated. Even for the default implementation, a setter method must be written to establish two-way data binding. This also didn't allow the use of default bindable properties.
A solution has now been found to address this gap using C# Expressions, without affecting performance or incurring runtime overhead as with reflection.
The resulting behavior will align with the classic model while being as extensible as Typed Binding.
The implementation will only take effect if the user has not defined a setter method. So it's backward compatible.
Motivation
Similar binding behavior across all approaches, permitting skill reuse.
Detailed Design
Defining a default setter method using C# Expressions:
if(setterisnull){// Include the setter only if it is defined as two-way or if the mode is overridden.if(targetProperty.DefaultBindingMode==BindingMode.TwoWay||mode==BindingMode.TwoWay){// Already defined in the toolkit to retrieve the member name - To be reused// Assuming MemberExpression for samplevarpropertyName=((MemberExpression)getter.Body).Member.Name;varparam1=Expression.Parameter(typeof(TBindingContext),"context");varparam2=Expression.Parameter(typeof(TSource),"value");varmemExp=Expression.Property(param1,propertyName);varassignExp=Expression.Assign(memExp,param2);// TODO: Need to check whether the action can be a static lambdavaraction=Expression.Lambda<Action<TBindingContext,TSource>>(assignExp,[param1,param2]).Compile();// Structure of the generated definition// setter = (TBindingContext context, TSource value) => context.Property = value;setter=action;}}
Usage Syntax
// As you can observe, the resulting code is quite simple. Easy to understand.// This will improve the overall productivity// Existing// Requires the target property even if it is the default one, and a setter definition if it is two-way binding.newPicker().Bind(Picker.SelectedIndexProperty,static(MyViewModelvm)=>vm.Index,static(MyViewModelvm,intvalue)=>vm.Index=value);// Proposed// SelectedIndexProperty is the default bindable property for Picker control// It's two-way binding by defaultnewPicker().Bind(static(MyViewModelvm)=>vm.Index);
Drawbacks
I don't think of any as it's compiled. Maybe a different perspective can fill-in.
Alternatives
Unresolved Questions
Static lambda for the setter action method to maintain the performance.
Explore the docs to ascertain whether the compiled method is static or if any further definition is required to make it static.
The text was updated successfully, but these errors were encountered:
For default bindable properties, method overloads can be written without the bindable property parameter. Once resolved, these overloads will call the existing implementation with the resolved value.
The setter method generated is type-safe, as it uses the generic type parameters of the method.
brminnick
added
needs discussion
The team will aim to discuss this at the next monthly standup
champion
A member of the .NET MAUI Toolkit core team has chosen to champion this feature
labels
Dec 4, 2024
Hey Vijay! Good news - this Proposal was approved in our January Standup!!
You are cleared to continue implementing this PR 🙌
That's great to hear. I began the work but got tied up with other commitments, so I couldn't focus much on it. Now that it's approved, I will prioritize this and submit the PR at the earliest.
approvedchampionA member of the .NET MAUI Toolkit core team has chosen to champion this featureproposalA fully fleshed out proposal describing a new feature in syntactic and semantic detail
Feature name
Automated Setter method for Two-Way Binding and Default BindableProperties
Link to discussion
#343
Progress tracker
Summary
The Bind method behaves differently in the Typed Bindings approach compared to the classic String-based one.
In the classic approach, it is enough to specify the property to be bound. If the binding property's mode is two-way, the back update to the ViewModel property will happen automatically (similar to that of an XAML-based definition).
In the improved Typed Bindings approach, getters and setters are separated. Even for the default implementation, a setter method must be written to establish two-way data binding. This also didn't allow the use of default bindable properties.
A solution has now been found to address this gap using C# Expressions, without affecting performance or incurring runtime overhead as with reflection.
The resulting behavior will align with the classic model while being as extensible as Typed Binding.
The implementation will only take effect if the user has not defined a setter method. So it's backward compatible.
Motivation
Similar binding behavior across all approaches, permitting skill reuse.
Detailed Design
Defining a default setter method using C# Expressions:
A working sample is available here - https://github.com/egvijayanand/markup-issue-272
Usage Syntax
Drawbacks
I don't think of any as it's compiled. Maybe a different perspective can fill-in.
Alternatives
Unresolved Questions
Static lambda for the setter action method to maintain the performance.
Explore the docs to ascertain whether the compiled method is static or if any further definition is required to make it static.
The text was updated successfully, but these errors were encountered: