1+ using System . Linq . Expressions ;
2+
3+ namespace FmgLib . MauiMarkup ;
4+
5+ public static partial class TypedBindingExtensions
6+ {
7+
8+ public static TBindable Bind < TBindable , TBindingContext , TSource > (
9+ this TBindable bindable ,
10+ BindableProperty targetProperty ,
11+ Expression < Func < TBindingContext , TSource > > getter ,
12+ Action < TBindingContext , TSource > ? setter = null ,
13+ BindingMode mode = BindingMode . Default ,
14+ string ? stringFormat = null ,
15+ TBindingContext ? source = default ) where TBindable : BindableObject
16+ {
17+ return Bind < TBindable , TBindingContext , TSource , object ? , object ? > (
18+ bindable ,
19+ targetProperty ,
20+ getter ,
21+ setter ,
22+ mode ,
23+ null ,
24+ null ,
25+ null ,
26+ stringFormat ,
27+ source ,
28+ null ,
29+ null ) ;
30+ }
31+
32+ public static TBindable Bind < TBindable , TBindingContext , TSource , TDest > (
33+ this TBindable bindable ,
34+ BindableProperty targetProperty ,
35+ Expression < Func < TBindingContext , TSource > > getter ,
36+ Action < TBindingContext , TSource > ? setter = null ,
37+ BindingMode mode = BindingMode . Default ,
38+ Func < TSource ? , TDest > ? convert = null ,
39+ Func < TDest ? , TSource > ? convertBack = null ,
40+ string ? stringFormat = null ,
41+ TBindingContext ? source = default ,
42+ TDest ? targetNullValue = default ,
43+ TDest ? fallbackValue = default ) where TBindable : BindableObject
44+ {
45+ return Bind < TBindable , TBindingContext , TSource , object ? , TDest > (
46+ bindable ,
47+ targetProperty ,
48+ getter ,
49+ setter ,
50+ mode ,
51+ convert is null ? null : ( source , _ ) => convert ( source ) ,
52+ convertBack is null ? null : ( dest , _ ) => convertBack ( dest ) ,
53+ null ,
54+ stringFormat ,
55+ source ,
56+ targetNullValue ,
57+ fallbackValue ) ;
58+ }
59+
60+ public static TBindable Bind < TBindable , TBindingContext , TSource , TDest > (
61+ this TBindable bindable ,
62+ BindableProperty targetProperty ,
63+ Expression < Func < TBindingContext , TSource > > getter ,
64+ Action < TBindingContext , TSource > ? setter = null ,
65+ BindingMode mode = BindingMode . Default ,
66+ IValueConverter ? converter = null ,
67+ string ? stringFormat = null ,
68+ TBindingContext ? source = default ,
69+ TDest ? targetNullValue = default ,
70+ TDest ? fallbackValue = default ) where TBindable : BindableObject
71+ {
72+ return Bind < TBindable , TBindingContext , TSource , object ? , TDest > (
73+ bindable ,
74+ targetProperty ,
75+ getter ,
76+ setter ,
77+ mode ,
78+ converter ,
79+ null ,
80+ stringFormat ,
81+ source ,
82+ targetNullValue ,
83+ fallbackValue ) ;
84+ }
85+
86+ public static TBindable Bind < TBindable , TBindingContext , TSource , TDest > (
87+ this TBindable bindable ,
88+ BindableProperty targetProperty ,
89+ Func < TBindingContext , TSource > getter ,
90+ ( Func < TBindingContext , object ? > , string ) [ ] handlers ,
91+ Action < TBindingContext , TSource > ? setter = null ,
92+ BindingMode mode = BindingMode . Default ,
93+ IValueConverter ? converter = null ,
94+ string ? stringFormat = null ,
95+ TBindingContext ? source = default ,
96+ TDest ? targetNullValue = default ,
97+ TDest ? fallbackValue = default ) where TBindable : BindableObject
98+ {
99+ return Bind < TBindable , TBindingContext , TSource , object ? , TDest > (
100+ bindable ,
101+ targetProperty ,
102+ getter ,
103+ handlers ,
104+ setter ,
105+ mode ,
106+ converter ,
107+ null ,
108+ stringFormat ,
109+ source ,
110+ targetNullValue ,
111+ fallbackValue ) ;
112+ }
113+
114+ public static TBindable Bind < TBindable , TBindingContext , TSource , TParam , TDest > (
115+ this TBindable bindable ,
116+ BindableProperty targetProperty ,
117+ Expression < Func < TBindingContext , TSource > > getter ,
118+ Action < TBindingContext , TSource > ? setter = null ,
119+ BindingMode mode = BindingMode . Default ,
120+ Func < TSource ? , TParam ? , TDest > ? convert = null ,
121+ Func < TDest ? , TParam ? , TSource > ? convertBack = null ,
122+ TParam ? converterParameter = default ,
123+ string ? stringFormat = null ,
124+ TBindingContext ? source = default ,
125+ TDest ? targetNullValue = default ,
126+ TDest ? fallbackValue = default ) where TBindable : BindableObject
127+ {
128+ var getterFunc = ConvertExpressionToFunc ( getter ) ;
129+
130+ return Bind (
131+ bindable ,
132+ targetProperty ,
133+ getterFunc ,
134+ new ( Func < TBindingContext , object ? > , string ) [ ] { ( ( TBindingContext b ) => b , GetMemberName ( getter ) ) } ,
135+ setter ,
136+ mode ,
137+ convert ,
138+ convertBack ,
139+ converterParameter ,
140+ stringFormat ,
141+ source ,
142+ targetNullValue ,
143+ fallbackValue ) ;
144+ }
145+
146+ public static TBindable Bind < TBindable , TBindingContext , TSource , TParam , TDest > (
147+ this TBindable bindable ,
148+ BindableProperty targetProperty ,
149+ Expression < Func < TBindingContext , TSource > > getter ,
150+ Action < TBindingContext , TSource > ? setter = null ,
151+ BindingMode mode = BindingMode . Default ,
152+ IValueConverter ? converter = null ,
153+ TParam ? converterParameter = default ,
154+ string ? stringFormat = null ,
155+ TBindingContext ? source = default ,
156+ TDest ? targetNullValue = default ,
157+ TDest ? fallbackValue = default ) where TBindable : BindableObject
158+ {
159+ var getterFunc = ConvertExpressionToFunc ( getter ) ;
160+
161+ return Bind (
162+ bindable ,
163+ targetProperty ,
164+ getterFunc ,
165+ new ( Func < TBindingContext , object ? > , string ) [ ] { ( ( TBindingContext b ) => b , GetMemberName ( getter ) ) } ,
166+ setter ,
167+ mode ,
168+ converter ,
169+ converterParameter ,
170+ stringFormat ,
171+ source ,
172+ targetNullValue ,
173+ fallbackValue ) ;
174+ }
175+
176+ static Func < TBindingContext , TSource > ConvertExpressionToFunc < TBindingContext , TSource > ( in Expression < Func < TBindingContext , TSource > > expression ) => expression . Compile ( ) ;
177+
178+ static string GetMemberName < T > ( in Expression < T > expression ) => expression . Body switch
179+ {
180+ MemberExpression m => m . Member . Name ,
181+ UnaryExpression u when u . Operand is MemberExpression m => m . Member . Name ,
182+ _ => throw new InvalidOperationException ( "Could not retreive member name" )
183+ } ;
184+ }
0 commit comments