@@ -41,7 +41,8 @@ namespace Caliburn.Micro
4141 /// <summary>
4242 /// Binds a view to a view model.
4343 /// </summary>
44- public static class ViewModelBinder {
44+ public static class ViewModelBinder
45+ {
4546 const string AsyncSuffix = "Async" ;
4647
4748 static readonly ILog Log = LogManager . GetLog ( typeof ( ViewModelBinder ) ) ;
@@ -75,7 +76,8 @@ public static class ViewModelBinder {
7576 /// </summary>
7677 /// <param name="view">The view to check.</param>
7778 /// <returns>Whether or not conventions should be applied to the view.</returns>
78- public static bool ShouldApplyConventions ( FrameworkElement view ) {
79+ public static bool ShouldApplyConventions ( FrameworkElement view )
80+ {
7981 var overriden = View . GetApplyConventions ( view ) ;
8082 return overriden . GetValueOrDefault ( ApplyConventionsByDefault ) ;
8183 }
@@ -84,30 +86,35 @@ public static bool ShouldApplyConventions(FrameworkElement view) {
8486 /// Creates data bindings on the view's controls based on the provided properties.
8587 /// </summary>
8688 /// <remarks>Parameters include named Elements to search through and the type of view model to determine conventions for. Returns unmatched elements.</remarks>
87- public static Func < IEnumerable < FrameworkElement > , Type , IEnumerable < FrameworkElement > > BindProperties = ( namedElements , viewModelType ) => {
89+ public static Func < IEnumerable < FrameworkElement > , Type , IEnumerable < FrameworkElement > > BindProperties = ( namedElements , viewModelType ) =>
90+ {
8891
8992 var unmatchedElements = new List < FrameworkElement > ( ) ;
9093#if ! XFORMS && ! MAUI
91- foreach ( var element in namedElements ) {
94+ foreach ( var element in namedElements )
95+ {
9296 var cleanName = element . Name . Trim ( '_' ) ;
9397 var parts = cleanName . Split ( new [ ] { '_' } , StringSplitOptions . RemoveEmptyEntries ) ;
9498
9599 var property = viewModelType . GetPropertyCaseInsensitive ( parts [ 0 ] ) ;
96100 var interpretedViewModelType = viewModelType ;
97101
98- for ( int i = 1 ; i < parts . Length && property != null ; i ++ ) {
102+ for ( int i = 1 ; i < parts . Length && property != null ; i ++ )
103+ {
99104 interpretedViewModelType = property . PropertyType ;
100105 property = interpretedViewModelType . GetPropertyCaseInsensitive ( parts [ i ] ) ;
101106 }
102107
103- if ( property == null ) {
108+ if ( property == null )
109+ {
104110 unmatchedElements . Add ( element ) ;
105111 Log . Info ( "Binding Convention Not Applied: Element {0} did not match a property." , element . Name ) ;
106112 continue ;
107113 }
108114
109115 var convention = ConventionManager . GetElementConvention ( element . GetType ( ) ) ;
110- if ( convention == null ) {
116+ if ( convention == null )
117+ {
111118 unmatchedElements . Add ( element ) ;
112119 Log . Warn ( "Binding Convention Not Applied: No conventions configured for {0}." , element . GetType ( ) ) ;
113120 continue ;
@@ -121,10 +128,12 @@ public static bool ShouldApplyConventions(FrameworkElement view) {
121128 convention
122129 ) ;
123130
124- if ( applied ) {
131+ if ( applied )
132+ {
125133 Log . Info ( "Binding Convention Applied: Element {0}." , element . Name ) ;
126134 }
127- else {
135+ else
136+ {
128137 Log . Info ( "Binding Convention Not Applied: Element {0} has existing binding." , element . Name ) ;
129138 unmatchedElements . Add ( element ) ;
130139 }
@@ -138,29 +147,33 @@ public static bool ShouldApplyConventions(FrameworkElement view) {
138147 /// Attaches instances of <see cref="ActionMessage"/> to the view's controls based on the provided methods.
139148 /// </summary>
140149 /// <remarks>Parameters include the named elements to search through and the type of view model to determine conventions for. Returns unmatched elements.</remarks>
141- public static Func < IEnumerable < FrameworkElement > , Type , IEnumerable < FrameworkElement > > BindActions = ( namedElements , viewModelType ) => {
150+ public static Func < IEnumerable < FrameworkElement > , Type , IEnumerable < FrameworkElement > > BindActions = ( namedElements , viewModelType ) =>
151+ {
142152 var unmatchedElements = namedElements . ToList ( ) ;
143153#if ! XFORMS && ! MAUI
144154#if WINDOWS_UWP || XFORMS || MAUI
145155 var methods = viewModelType . GetRuntimeMethods ( ) ;
146156#else
147157 var methods = viewModelType . GetMethods ( ) ;
148158#endif
149-
150159
151- foreach ( var method in methods ) {
152- Log . Info ( $ "Searching for methods control { method . Name } unmatchedElements count { unmatchedElements . Count } ") ;
160+
161+ foreach ( var method in methods )
162+ {
163+ Log . Info ( $ "Searching for methods control { method . Name } unmatchedElements count { unmatchedElements . Count } ") ;
153164 var foundControl = unmatchedElements . FindName ( method . Name ) ;
154- if ( foundControl == null && IsAsyncMethod ( method ) ) {
165+ if ( foundControl == null && IsAsyncMethod ( method ) )
166+ {
155167 var methodNameWithoutAsyncSuffix = method . Name . Substring ( 0 , method . Name . Length - AsyncSuffix . Length ) ;
156168 foundControl = unmatchedElements . FindName ( methodNameWithoutAsyncSuffix ) ;
157169 }
158170
159- if ( foundControl == null ) {
171+ if ( foundControl == null )
172+ {
160173 Log . Info ( "Action Convention Not Applied: No actionable element for {0}. {1}" , method . Name , unmatchedElements . Count ) ;
161- foreach ( var element in unmatchedElements )
174+ foreach ( var element in unmatchedElements )
162175 {
163- Log . Info ( $ "Unnamed element { element . Name } ") ;
176+ Log . Info ( $ "Unnamed element { element . Name } ") ;
164177 }
165178 continue ;
166179 }
@@ -179,10 +192,12 @@ public static bool ShouldApplyConventions(FrameworkElement view) {
179192 var message = method . Name ;
180193 var parameters = method . GetParameters ( ) ;
181194
182- if ( parameters . Length > 0 ) {
195+ if ( parameters . Length > 0 )
196+ {
183197 message += "(" ;
184198
185- foreach ( var parameter in parameters ) {
199+ foreach ( var parameter in parameters )
200+ {
186201 var paramName = parameter . Name ;
187202 var specialValue = "$" + paramName . ToLower ( ) ;
188203
@@ -203,7 +218,8 @@ public static bool ShouldApplyConventions(FrameworkElement view) {
203218 return unmatchedElements ;
204219 } ;
205220
206- static bool IsAsyncMethod ( MethodInfo method ) {
221+ static bool IsAsyncMethod ( MethodInfo method )
222+ {
207223 return typeof ( Task ) . GetTypeInfo ( ) . IsAssignableFrom ( method . ReturnType . GetTypeInfo ( ) ) &&
208224 method . Name . EndsWith ( AsyncSuffix , StringComparison . OrdinalIgnoreCase ) ;
209225 }
@@ -217,19 +233,8 @@ static bool IsAsyncMethod(MethodInfo method) {
217233 /// Binds the specified viewModel to the view.
218234 /// </summary>
219235 ///<remarks>Passes the the view model, view and creation context (or null for default) to use in applying binding.</remarks>
220- public static Action < object , DependencyObject , object > Bind = ( viewModel , view , context ) => {
221- #if ! WINDOWS_UWP && ! XFORMS && ! MAUI
222- // when using d:DesignInstance, Blend tries to assign the DesignInstanceExtension class as the DataContext,
223- // so here we get the actual ViewModel which is in the Instance property of DesignInstanceExtension
224- if ( View . InDesignMode ) {
225- var vmType = viewModel . GetType ( ) ;
226- if ( vmType . FullName == "Microsoft.Expression.DesignModel.InstanceBuilders.DesignInstanceExtension" ) {
227- var propInfo = vmType . GetProperty ( "Instance" , BindingFlags . Instance | BindingFlags . NonPublic ) ;
228- viewModel = propInfo . GetValue ( viewModel , null ) ;
229- }
230- }
231- #endif
232-
236+ public static Action < object , DependencyObject , object > Bind = ( viewModel , view , context ) =>
237+ {
233238 Log . Info ( "Binding {0} and {1}." , view , viewModel ) ;
234239
235240#if XFORMS
@@ -240,29 +245,35 @@ static bool IsAsyncMethod(MethodInfo method) {
240245 var noContext = Caliburn . Micro . Bind . NoContextProperty ;
241246#endif
242247
243- if ( ( bool ) view . GetValue ( noContext ) ) {
248+ if ( ( bool ) view . GetValue ( noContext ) )
249+ {
244250 Action . SetTargetWithoutContext ( view , viewModel ) ;
245251 }
246- else {
252+ else
253+ {
247254 Action . SetTarget ( view , viewModel ) ;
248255 }
249256
250257 var viewAware = viewModel as IViewAware ;
251- if ( viewAware != null ) {
258+ if ( viewAware != null )
259+ {
252260 Log . Info ( "Attaching {0} to {1}." , view , viewAware ) ;
253261 viewAware . AttachView ( view , context ) ;
254262 }
255263
256- if ( ( bool ) view . GetValue ( ConventionsAppliedProperty ) ) {
264+ if ( ( bool ) view . GetValue ( ConventionsAppliedProperty ) )
265+ {
257266 return ;
258267 }
259268
260269 var element = View . GetFirstNonGeneratedView ( view ) as FrameworkElement ;
261- if ( element == null ) {
270+ if ( element == null )
271+ {
262272 return ;
263273 }
264274
265- if ( ! ShouldApplyConventions ( element ) ) {
275+ if ( ! ShouldApplyConventions ( element ) )
276+ {
266277 Log . Info ( "Skipping conventions for {0} and {1}." , element , viewModel ) ;
267278 return ;
268279 }
0 commit comments