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
blockedchampionA 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
Add RelativeBindingSource Support to Typed Bindings
Link to discussion
None
Progress tracker
Android Implementation
iOS Implementation
MacCatalyst Implementation
Windows Implementation
Tizen Implementation
Unit Tests
Samples
Documentation
Summary
This Proposal improves support for RelativeBindingSource to our TypedBinding extensions.
This is not a breaking change and improves our API surface to better match .NET MAUI's TypedBinding API for its TypedBinding.Source property.
RelativeBindingSource is the recommended way for .NET MAUI developers to creating a binding referencing themselves or an ancestor without needing to pass in an instance of that object
Self Binding
RelativeBindingSource.Self
Binding to an ancestor in the UI hierarchy
new RelativeBindingSource(RelativeBindingSourceMode.FindAncestor, typeof(Page)))
Binding to an ancestor in the BindingContext hierarchy
new RelativeBindingSource(RelativeBindingSourceMode.FindAncestorBindingContext, typeof(MyViewModel))
Current Workaround
The current workaround to use RelativeBindingSource with our existing TypedBinding APIs is to first use .Assign() to assign the control to a variable, then pass that variable in as the source parameter.
All of our TypedBinding APIs (example below) require the source parameter type to match the TBindingContext used in by the getter and setter parameter:
Expression<Func<TBindingContext, TSource>> getter
Action<TBindingContext, TSource>? setter = null
TBindingContext? source = default // Cannot be of type RelativeBindingSource
/// <summary>Bind to a specified property</summary>publicstaticTBindableBind<TBindable,TBindingContext,TSource>(thisTBindablebindable,BindablePropertytargetProperty,Expression<Func<TBindingContext,TSource>>getter,Action<TBindingContext,TSource>?setter=null,BindingModemode=BindingMode.Default,string?stringFormat=null,object?source=null)// Lowered from TBindingContext? source = defaultwhereTBindable:BindableObject;
We will need to update our Unit Tests to validate RelativeBindingSource.
We will also need to update our Unit Tests with invalid source parameters to ensure .NET MAUI throws an exception if a user provides an invalid source parameter to our API now that it supports any object type.
Alternatives
An alternative to lowering our existing API surface from TBindingContext? source = default to object? source = null would be to create new APIs specific to RelativeBindingSource (example below).
I do not recommend this approach for two reasons:
It doubles our API surface for TypedBinding support, increasing our maintenance cost
Requires one TypedBinding API for TBindingContext? source = default and a second TypedBinding API for object? source = null
blockedchampionA 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
Add
RelativeBindingSource
Support to Typed BindingsLink to discussion
None
Progress tracker
Summary
This Proposal improves support for
RelativeBindingSource
to our TypedBinding extensions.This is not a breaking change and improves our API surface to better match .NET MAUI's TypedBinding API for its
TypedBinding.Source
property.RelativeBindingSource
is the recommended way for .NET MAUI developers to creating a binding referencing themselves or an ancestor without needing to pass in an instance of that objectRelativeBindingSource.Self
new RelativeBindingSource(RelativeBindingSourceMode.FindAncestor, typeof(Page))
)new RelativeBindingSource(RelativeBindingSourceMode.FindAncestorBindingContext, typeof(MyViewModel))
Current Workaround
The current workaround to use
RelativeBindingSource
with our existing TypedBinding APIs is to first use.Assign()
to assign the control to a variable, then pass that variable in as thesource
parameter.Motivation
All of our TypedBinding APIs (example below) require the
source
parameter type to match theTBindingContext
used in by thegetter
andsetter
parameter:Expression<Func<TBindingContext, TSource>> getter
Action<TBindingContext, TSource>? setter = null
TBindingContext? source = default
// Cannot be of typeRelativeBindingSource
Detailed Design
The best way to support
RelativeBindingSource
, is to lower theType
of the API'ssource
parameter fromTBindingContext?
toobject?
.Using
object?
for thesource
parameter matches the .NET MAUI API for theTypedBinding.Source
property.Current Source Parameter
Updated Source Parameter
Example Updated API
Usage Syntax
Drawbacks
I don't see any drawbacks.
We will need to update our Unit Tests to validate
RelativeBindingSource
.We will also need to update our Unit Tests with invalid
source
parameters to ensure .NET MAUI throws an exception if a user provides an invalidsource
parameter to our API now that it supports anyobject
type.Alternatives
An alternative to lowering our existing API surface from
TBindingContext? source = default
toobject? source = null
would be to create new APIs specific toRelativeBindingSource
(example below).I do not recommend this approach for two reasons:
TBindingContext? source = default
and a second TypedBinding API forobject? source = null
object?
better matches .NET MAUI's TypedBinding.Source API (public object? Source
)Example API using
RelativeBindingSource
Unresolved Questions
None
The text was updated successfully, but these errors were encountered: