CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
Csla.Axml.Android/Binding/Binding.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="Binding.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Contains an individual binding to tie a property on an Axml view to the property on a supplied object</summary>
7//-----------------------------------------------------------------------
8
9using System;
10using System.ComponentModel;
11using Android.Views;
12
14{
18 public class Binding : IDisposable
19 {
23 public View Target { get; private set; }
24
28 public object Source { get; private set; }
29
33 public System.Reflection.PropertyInfo TargetProperty { get; private set; }
34
38 public System.Reflection.PropertyInfo SourceProperty { get; private set; }
39
43 public Func<object, object> Convert { get; private set; }
44
48 public Func<object, object> ConvertBack { get; private set; }
49
57 public Binding(View target, string targetProperty, object source, string sourceProperty)
58 : this(target, targetProperty, source, sourceProperty, null, null)
59 { }
60
69 public Binding(View target, string targetProperty, object source, string sourceProperty, Func<object, object> convert)
70 : this(target, targetProperty, source, sourceProperty, convert, null)
71 { }
72
82 public Binding(View target, string targetProperty, object source, string sourceProperty, Func<object, object> convert, Func<object, object> convertBack)
83 {
84 Target = target;
85 Source = source;
86 TargetProperty = Target.GetType().GetProperty(targetProperty);
87 SourceProperty = Source.GetType().GetProperty(sourceProperty);
88 Convert = convert;
89 ConvertBack = convertBack;
90
91 Target.FocusChange += Target_FocusChange;
92
93 var inpc = Source as INotifyPropertyChanged;
94 if (inpc != null)
95 inpc.PropertyChanged += Source_PropertyChanged;
96
98 }
99
105 void Source_PropertyChanged(object sender, PropertyChangedEventArgs e)
106 {
107 UpdateTarget();
108 }
109
115 void Target_FocusChange(object sender, View.FocusChangeEventArgs e)
116 {
117 if (!e.HasFocus)
118 UpdateSource();
119 }
120
124 public void UpdateSource()
125 {
126 var value = TargetProperty.GetValue(Target, null);
127 if (ConvertBack != null)
128 value = ConvertBack(value);
129 var converted = Utilities.CoerceValue(
130 SourceProperty.PropertyType, TargetProperty.PropertyType, null, value);
131 SourceProperty.SetValue(Source,
132 converted,
133 null);
134 }
135
139 public void UpdateTarget()
140 {
141 var value = SourceProperty.GetValue(Source, null);
142 if (Convert != null)
143 value = Convert(value);
144 var converted = Utilities.CoerceValue(
145 TargetProperty.PropertyType, SourceProperty.PropertyType, null, value);
146 TargetProperty.SetValue(Target,
147 converted,
148 null);
149 }
150
154 public void Dispose()
155 {
156 Target.FocusChange -= Target_FocusChange;
157
158 var inpc = Source as INotifyPropertyChanged;
159 if (inpc != null)
160 inpc.PropertyChanged -= Source_PropertyChanged;
161 Target = null;
162 Source = null;
163 TargetProperty = null;
164 SourceProperty = null;
165 }
166 }
167}
Contains an individual binding to tie a property on an Axml view to the property on a supplied object...
object Source
The object that the view is bound to.
Binding(View target, string targetProperty, object source, string sourceProperty, Func< object, object > convert, Func< object, object > convertBack)
Creates a new instance of the Binding class.
void UpdateTarget()
Updates the target with the current value in the source object.
System.Reflection.PropertyInfo SourceProperty
The PropertyInfo for the property on the source object that is being bound to.
View Target
The Axml view that is used by the binding.
Binding(View target, string targetProperty, object source, string sourceProperty, Func< object, object > convert)
Creates a new instance of the Binding class.
void Dispose()
Clears the bindings, references and event handlers.
System.Reflection.PropertyInfo TargetProperty
The PropertyInfo for the property on the target view that is being bound to.
void UpdateSource()
Updates the source with the current value in the target object.
Func< object, object > Convert
A reference to a function to do a custom conversion from the source property to the target property.
Func< object, object > ConvertBack
A reference to a function to do a custom conversion from the target property to the source property.
Binding(View target, string targetProperty, object source, string sourceProperty)
Creates a new instance of the Binding class.
PropertyInfo(string name)
Creates a new instance of this class.