[Proposal] Add `RelativeBindingSource` Support to Typed Bindings Extensions
维护者通常 1 天内回复
评估
这个 Issue 还没有评估数据。
描述
Feature name
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.
.Assign(out CarouselView carouselView)
.Bind(CarouselView.CurrentItemChangedCommandParameterProperty, static (CarouselView view) => view.CurrentItem, source: carouselView)
Motivation
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>> getterAction<TBindingContext, TSource>? setter = nullTBindingContext? source = default// Cannot be of typeRelativeBindingSource
public static TBindable Bind<TBindable, TBindingContext, TSource>(
this TBindable bindable,
BindableProperty targetProperty,
Expression<Func<TBindingContext, TSource>> getter,
Action<TBindingContext, TSource>? setter = null,
BindingMode mode = BindingMode.Default,
string? stringFormat = null,
TBindingContext? source = default) where TBindable : BindableObject);
Detailed Design
The best way to support RelativeBindingSource, is to lower the Type of the API's source parameter from TBindingContext? to object?.
Using object? for the source parameter matches the .NET MAUI API for the TypedBinding.Source property.
Current Source Parameter
TBindingContext? source = default
Updated Source Parameter
object? source = null
Example Updated API
/// <summary>Bind to a specified property</summary>
public static TBindable Bind<TBindable, TBindingContext, TSource>(
this TBindable bindable,
BindableProperty targetProperty,
Expression<Func<TBindingContext, TSource>> getter,
Action<TBindingContext, TSource>? setter = null,
BindingMode mode = BindingMode.Default,
string? stringFormat = null,
object? source = null) // Lowered from TBindingContext? source = default
where TBindable : BindableObject;
Usage Syntax
.Bind(CarouselView.CurrentItemChangedCommandParameterProperty, static (CarouselView view) => view.CurrentItem, source: RelativeBindingSource.Self)
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 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 = defaultand a second TypedBinding API forobject? source = null
- Requires one TypedBinding API for
- Lowering the parameter Type to
object?better matches .NET MAUI's TypedBinding.Source API (public object? Source)
Example API using RelativeBindingSource
public static TBindable Bind<TBindable, TBindingContext, TSource>(
this TBindable bindable,
BindableProperty targetProperty,
Expression<Func<TBindingContext, TSource>> getter,
Action<TBindingContext, TSource>? setter = null,
BindingMode mode = BindingMode.Default,
string? stringFormat = null,
RelativeBindingSource? source = null) where TBindable : BindableObject);
Unresolved Questions
None
- 主要语言
- C#
- 星标
- 597
- 派生
- 39
- 平均合并
- 9 小时 36 分钟
- 30 天内合并 PR
- 12
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
CommunityToolkit/Maui.Markup 的其他 Issue
-
bug unverified
难度 4/5 3-5 天 新手友好度 35/100
CommunityToolkit/Maui.Markup#410 · 1 条评论 ·
维护者通常 1 天内回复
-
[Proposal] Automated Setter method for Two-Way Binding and Default BindableProperties可能重新可做 @egvijayanand 于 717 天前认领,目前没有进行中的 PR。 未关闭approved champion proposal
CommunityToolkit/Maui.Markup#345 · 6 条评论 · 已指派 2 人 ·
维护者通常 1 天内回复
查看 CommunityToolkit/Maui.Markup 的全部 Issue
相似的 Issue
-
area: core bug help wanted ready
难度 2/5 1-3 小时 新手友好度 62/100
维护者通常 1 天内回复
-
area: remote bug
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
-
📅 Sprint 4 Module-Tools Setup Submit:None 🦔 Size Tiny
难度 2/5 1-3 小时 新手友好度 70/100
CodeYourFuture/Module-Tools#712 ·
-
:watch: Not Triaged dotnet-fundamentals/svc
难度 1/5 1 小时以内 新手友好度 88/100
维护者通常 1 天内回复
-
A: Mapping P3: Standard T: New Feature
难度 2/5 1-3 小时 新手友好度 68/100
space-wizards/space-station-14#46878 · 2 条评论 ·
维护者通常 1 天内回复