Beispiel #1
0
        public static Action Bind(this UISwitch toggle, INotifyPropertyChanged source, string propertyName)
        {
            var property = source.GetProperty(propertyName);

            if (property.PropertyType == typeof(bool))
            {
                toggle.SetValue(source, property);
                var handler = new PropertyChangedEventHandler ((s, e) =>
                {
                    if (e.PropertyName == propertyName)
                    {
                            toggle.InvokeOnMainThread(()=>
                                toggle.SetValue(source, property));
                    }
                });

                source.PropertyChanged += handler;

                var valueChanged = new EventHandler(
                    (sender, e) => property.GetSetMethod().Invoke (source, new object[]{ toggle.On }));

                toggle.ValueChanged += valueChanged;

                return new Action(() =>
                {
                    source.PropertyChanged -= handler;
                    toggle.ValueChanged -= valueChanged;
                });
            } 
            else
            {
                throw new InvalidCastException ("Binding property is not boolean");
            }
        }
Beispiel #2
0
        public static PropertyChangedEventHandler Bind(this CompoundButton toggle, INotifyPropertyChanged source, string propertyName)
        {
            var property = source.GetProperty(propertyName);

            if (property.PropertyType == typeof(bool))
            {
                toggle.SetValue(source, property);
                var handler = new PropertyChangedEventHandler((s, e) =>
                {
                    if (e.PropertyName == propertyName)
                    {
                        var activity = toggle.Context as Activity;

                        if (activity != null)
                        {
                            activity.RunOnUiThread(() => toggle.SetValue(source, property));
                        }
                    }
                });

                source.PropertyChanged += handler;
                toggle.CheckedChange   += (sender, e) => property.GetSetMethod().Invoke(source, new object[] { toggle.Checked });

                return(handler);
            }
            else
            {
                throw new InvalidCastException("Binding property is not boolean");
            }
        }
        public static Action Bind(this UITextField textField, INotifyPropertyChanged source, string propertyName)
        {
            var property = source.GetProperty(propertyName);

            textField.SetText(source, property);
            var handler = new PropertyChangedEventHandler((s, e) =>
                {
                    if (e.PropertyName == propertyName)
                    {
                        textField.InvokeOnMainThread(()=>
                            textField.SetText(source, property));
                    }
                });

            source.PropertyChanged += handler;

            var editHandler = new EventHandler((s, e) =>
                {
                    var sender = s as UITextField;
                    if (sender != null)
                    {
                        property.GetSetMethod().Invoke(source, new[] { sender.Text });
                    }
                });

            textField.EditingChanged += editHandler;

            // create an action to remove the event handlers
            return new Action(() =>
                {
                    textField.EditingChanged -= editHandler;
                    source.PropertyChanged -= handler;
                });
        }
Beispiel #4
0
        /// <summary>
        /// Bind two objects implementing INotifyPropertyChanged over given properties
        /// </summary>
        /// <param name="source">source object</param>
        /// <param name="sourcePropertyName">source property name</param>
        /// <param name="target">target object</param>
        /// <param name="targetPropertyName">target property name</param>
        /// <param name="bindmode">binding mode, e.g. BindingMode.TwoWay</param>
        public static void BindProperties(INotifyPropertyChanged source, string sourcePropertyName, INotifyPropertyChanged target, string targetPropertyName, BindingMode bindMode = BindingMode.TwoWay)
        {
            switch (bindMode)
            {
            case BindingMode.OneTime:
                target.SetProperty(targetPropertyName, source.GetProperty(sourcePropertyName)); //just set value once
                break;

            case BindingMode.OneWay:
                source.PropertyChanged += (s, e) => { if (e.PropertyName == sourcePropertyName)
                                                      {
                                                          target.SetProperty(targetPropertyName, source.GetProperty(sourcePropertyName));
                                                      }
                };                                                                                           //bind forward...
                BindProperties(source, sourcePropertyName, target, targetPropertyName, BindingMode.OneTime); //...then set value (target may have looped back to us and changed our value, in that case we read that latest value from source and set to target)
                break;

            case BindingMode.TwoWay:
                target.PropertyChanged += (s, e) => { if (e.PropertyName == targetPropertyName)
                                                      {
                                                          source.SetProperty(sourcePropertyName, target.GetProperty(targetPropertyName));
                                                      }
                };                                                                                          //reverse bind
                BindProperties(source, sourcePropertyName, target, targetPropertyName, BindingMode.OneWay); //bind forward and then set value
                break;

            default:
                throw new NotSupportedException("Unsupported binding mode");
            }
        }
        public static PropertyChangedEventHandler Bind(this SeekBar seekBar, INotifyPropertyChanged source, string propertyName)
        {
            var property = source.GetProperty(propertyName);

            var r = property.GetCustomAttribute <RangeAttribute> ();

            if (r != null)
            {
                seekBar.Max = (int)r.Maximum;
            }

            var handler = new PropertyChangedEventHandler((s, e) =>
            {
                if (e.PropertyName == propertyName)
                {
                    //textField.SetText(source, property);
                }
            });

            source.PropertyChanged += handler;

            //textField.AfterTextChanged += (sender, e) => property.GetSetMethod().Invoke(source, new []{textField.Text});

            return(handler);
        }
Beispiel #6
0
        public static Action Bind(this UITextField textField, INotifyPropertyChanged source, string propertyName)
        {
            var property = source.GetProperty(propertyName);

            textField.SetText(source, property);
            var handler = new PropertyChangedEventHandler((s, e) =>
            {
                if (e.PropertyName == propertyName)
                {
                    textField.InvokeOnMainThread(() =>
                                                 textField.SetText(source, property));
                }
            });

            source.PropertyChanged += handler;

            var editHandler = new EventHandler((s, e) =>
            {
                var sender = s as UITextField;
                if (sender != null)
                {
                    property.GetSetMethod().Invoke(source, new[] { sender.Text });
                }
            });

            textField.EditingChanged += editHandler;

            // create an action to remove the event handlers
            return(new Action(() =>
            {
                textField.EditingChanged -= editHandler;
                source.PropertyChanged -= handler;
            }));
        }
        public static Action Bind(this UISwitch toggle, INotifyPropertyChanged source, string propertyName)
        {
            var property = source.GetProperty(propertyName);

            if (property.PropertyType == typeof(bool))
            {
                toggle.SetValue(source, property);
                var handler = new PropertyChangedEventHandler((s, e) =>
                {
                    if (e.PropertyName == propertyName)
                    {
                        toggle.InvokeOnMainThread(() =>
                                                  toggle.SetValue(source, property));
                    }
                });

                source.PropertyChanged += handler;

                var valueChanged = new EventHandler(
                    (sender, e) => property.GetSetMethod().Invoke(source, new object[] { toggle.On }));

                toggle.ValueChanged += valueChanged;

                return(new Action(() =>
                {
                    source.PropertyChanged -= handler;
                    toggle.ValueChanged -= valueChanged;
                }));
            }
            else
            {
                throw new InvalidCastException("Binding property is not boolean");
            }
        }
        public static PropertyChangedEventHandler Bind(this EditText textField, INotifyPropertyChanged source, string propertyName)
        {
            var property = source.GetProperty(propertyName);

            textField.SetText(source, property);

            var handler = new PropertyChangedEventHandler ((s, e) =>
                {
                    if (e.PropertyName == propertyName)
                    {
                        var activity = textField.Context as Activity;

                        if (activity != null)
                        {
                            activity.RunOnUiThread(() => textField.SetText(source, property));
                        }
                    }
                });

            source.PropertyChanged += handler;

            textField.AfterTextChanged += (sender, e) => property.GetSetMethod().Invoke(source, new []{textField.Text});

            return handler;
        }
Beispiel #9
0
        public static PropertyChangedEventHandler Bind(this EditText textField, INotifyPropertyChanged source, string propertyName)
        {
            var property = source.GetProperty(propertyName);

            textField.SetText(source, property);

            var handler = new PropertyChangedEventHandler((s, e) =>
            {
                if (e.PropertyName == propertyName)
                {
                    var activity = textField.Context as Activity;

                    if (activity != null)
                    {
                        activity.RunOnUiThread(() => textField.SetText(source, property));
                    }
                }
            });

            source.PropertyChanged += handler;

            textField.AfterTextChanged += (sender, e) => property.GetSetMethod().Invoke(source, new [] { textField.Text });

            return(handler);
        }
Beispiel #10
0
        public static Action Bind(
            this UILabel label,
            INotifyPropertyChanged source,
            string propertyName,
            IFormatProvider formatProvider = null)
        {
            var property = source.GetProperty(propertyName);

            var l = new WeakReference <UILabel> (label);

            label.SetText(source, property);

            var handler = new PropertyChangedEventHandler((s, e) =>
            {
                UILabel weakRef;
                if (e.PropertyName == propertyName && l.TryGetTarget(out weakRef))
                {
                    weakRef.InvokeOnMainThread(() => weakRef.SetText(source, property));
                }
            });

            source.PropertyChanged += handler;

            return(new Action(() => source.PropertyChanged -= handler));
        }
Beispiel #11
0
        public static PropertyChangedEventHandler Bind(this SeekBar seekBar, INotifyPropertyChanged source, string propertyName)
        {
            var property = source.GetProperty(propertyName);

            var r = property.GetCustomAttribute<RangeAttribute> ();

            if (r != null)
            {
                seekBar.Max = (int)r.Maximum;
            }

            var handler = new PropertyChangedEventHandler ((s, e) =>
                {
                    if (e.PropertyName == propertyName)
                    {
                        //textField.SetText(source, property);
                    }
                });

            source.PropertyChanged += handler;

            //textField.AfterTextChanged += (sender, e) => property.GetSetMethod().Invoke(source, new []{textField.Text});

            return handler;
        }
        public static PropertyChangedEventHandler Bind(this CompoundButton toggle, INotifyPropertyChanged source, string propertyName)
        {
            var property = source.GetProperty(propertyName);

            if (property.PropertyType == typeof(bool))
            {
                toggle.SetValue(source, property);
                var handler = new PropertyChangedEventHandler ((s, e) =>
                    {
                        if (e.PropertyName == propertyName)
                        {
                            var activity = toggle.Context as Activity;

                            if (activity != null)
                            {
                                activity.RunOnUiThread(() => toggle.SetValue(source, property));
                            }
                        }
                    });

                source.PropertyChanged += handler;
                toggle.CheckedChange += (sender, e) => property.GetSetMethod().Invoke (source, new object[]{ toggle.Checked });

                return handler;
            } 
            else
            {
                throw new InvalidCastException ("Binding property is not boolean");
            }
        }
        private static PropertyInfo GetProperty(INotifyPropertyChanged source, string propertyName)
        {
            var property = source.GetProperty(propertyName);

            if (property.PropertyType == typeof(int) || property.PropertyType == typeof(double) ||
                property.PropertyType == typeof(float))
            {
                return(property);
            }

            throw new InvalidCastException("Binding property is not valid");
        }
        public static void BindTitle(this Button button, INotifyPropertyChanged source, string propertyName)
        {
            var property = source.GetProperty(propertyName);

            button.SetTitle (source, property);

            source.PropertyChanged += (sender, e) =>
            {
                if (e.PropertyName == propertyName)
                {
                    var activity = button.Context as Activity;

                    if (activity != null)
                    {
                        activity.RunOnUiThread(() => button.SetTitle(source, property));
                    }
                }
            };
        }
Beispiel #15
0
        public static Action BindTitle(this UIButton button,
                                       INotifyPropertyChanged source,
                                       string propertyName,
                                       UIControlState state = UIControlState.Normal)
        {
            var property = source.GetProperty(propertyName);

            button.SetTitle(source, property, state);

            var handler = new PropertyChangedEventHandler(
                (s, e) =>
            {
                if (e.PropertyName == propertyName)
                {
                    button.SetTitle(source, property, state);
                }
            }
                );

            source.PropertyChanged += handler;

            return(new Action(() => source.PropertyChanged -= handler));
        }
        public static Action BindTitle(this UIButton button, 
                    INotifyPropertyChanged source, 
                    string propertyName,
                    UIControlState state = UIControlState.Normal)
        {
            var property = source.GetProperty(propertyName);

            button.SetTitle (source, property, state);

            var handler = new PropertyChangedEventHandler(
                (s, e) =>
                {
                    if (e.PropertyName == propertyName)
                    {
                        button.SetTitle (source, property, state);
                    }
                }
            );

            source.PropertyChanged += handler;

            return new Action(() => source.PropertyChanged -= handler);
        }
        public static Action Bind(
            this UILabel label, 
            INotifyPropertyChanged source, 
            string propertyName, 
            IFormatProvider formatProvider = null)
        {
            var property = source.GetProperty(propertyName);

            var l = new WeakReference<UILabel> (label);
            label.SetText(source, property);

            var handler = new PropertyChangedEventHandler((s, e) =>
            {
                UILabel weakRef;
                if (e.PropertyName == propertyName && l.TryGetTarget(out weakRef))
                {
                    weakRef.InvokeOnMainThread(()=> weakRef.SetText(source, property));
                }
            });

            source.PropertyChanged += handler;

            return new Action(() => source.PropertyChanged -= handler);
        }
Beispiel #18
0
        private static PropertyInfo GetProperty(INotifyPropertyChanged source, string propertyName)
        {
            var property = source.GetProperty(propertyName);

            if (property.PropertyType == typeof(int) || property.PropertyType == typeof(double)
                || property.PropertyType == typeof(float))
            {
                return property;
            }

            throw new InvalidCastException ("Binding property is not valid");
        }