internal static void HandleViewValueChanged <TView, TArgs, TNewValue>( BindingExpression bindingExpression, Func <TView, TArgs, TNewValue> newValueFunc, BindingRegistration bindingRegistration, IValueConverter converter, TArgs args) #if __ANDROID__ || MONODROID where TView : Android.Views.View #endif { try { var rawValue = newValueFunc((TView)bindingExpression.View, args); UpdateSourceProperty( bindingExpression, bindingRegistration, rawValue, converter, bindingExpression.ConverterParameter); } catch #if DEBUG (Exception ex) #endif { /* TODO: log exception */ if (Debugger.IsAttached) { Debugger.Break(); } } }
public object Get(Type service, string name = null, params IIocParameter[] parameters) { this.NotBeDisposed(); Should.NotBeNull(service, nameof(service)); var key = new BindingKey(service, name); BindingRegistration registration = null; lock (_bindingRegistrations) { List <BindingRegistration> list; if (_bindingRegistrations.TryGetValue(key, out list)) { if (list.Count > 1) { throw new InvalidOperationException($"Cannot activate type {service} found more that one binding"); } if (list.Count == 1) { registration = list[0]; } } } if (registration != null) { return(registration.Resolve(parameters)); } if (_parent != null && _parent.HasRegistration(key)) { return(_parent.Get(service, name, parameters)); } return(Resolve(service, parameters)); }
public Action BindView(BindingExpression bindingExpression, BindingRegistration bindingRegistration, IValueConverter converter) { EventHandler <TArgs> handler = (sender, args) => { ViewValueChangedHandler.HandleViewValueChanged(bindingExpression, newValueFunc, bindingRegistration, converter, args); }; addHandler((TView)bindingExpression.View, handler); Action removeAction = () => { removeHandler((TView)bindingExpression.View, handler); }; return(removeAction); }
private bool TryResolveSelfBindable(Type service, IIocParameter[] parameters, out object value) { BindingRegistration registration; lock (_selfActivatedRegistrations) { if (!_selfActivatedRegistrations.TryGetValue(service, out registration)) { if (IsSelfBindableType(service)) { registration = new BindingRegistration(this, service, DependencyLifecycle.TransientInstance, Empty.Array <IIocParameter>()); } _selfActivatedRegistrations[service] = registration; } } if (registration == null) { value = null; return(false); } value = registration.Resolve(parameters); return(true); }
private void ApplyBinding( BindingExpression bindingExpression, IValueConverter converter) { bool oppositeOneWay = false; PropertyInfo targetProperty = null; if (bindingExpression.Target == "Strikethrough" && bindingExpression.View is TextView tv) { targetProperty = typeof(TextViewStrikethroughWrapper).GetProperty(nameof(TextViewStrikethroughWrapper.Strikethrough)); } else if (bindingExpression.Target == "HasFocus") { oppositeOneWay = true; } else { targetProperty = bindingExpression.View.GetType().GetProperty(bindingExpression.Target); } if (targetProperty == null && !oppositeOneWay) { string exMessage = "targetProperty on View could not be found. View: " + bindingExpression.View.GetType() + ". Target: " + bindingExpression.Target; if (Debugger.IsAttached) { Debugger.Break(); } throw new KeyNotFoundException(exMessage); } // We try localizing, otherwise we bind if (!TryHandleLocalizationBinding(bindingExpression, targetProperty, converter)) { BindingRegistration bindingRegistration = null; if (oppositeOneWay) { bindingRegistration = BindingHost.GetEmptyRegistration(bindingExpression.Source); } else { Action <object> bindingCallback = value => { try { SetTargetProperty( rawValue: value, view: bindingExpression.View, targetProperty, converter, bindingExpression.ConverterParameter); } catch (Exception ex) { // View is disposed, should unregister if (ex is TargetInvocationException && ex.InnerException is ObjectDisposedException) { // Note that don't need to call unbind action on the two way view binder since view is already disposed bindingRegistration?.Unregister(); } else { if (Debugger.IsAttached) { Debugger.Break(); } } } }; bindingRegistration = BindingHost.SetBinding(bindingExpression.Source, bindingCallback); } if (bindingExpression.Mode == BindingMode.TwoWay) { if (ViewBinderRegistry.TryGetViewBinder(bindingExpression.View.GetType(), bindingExpression.Target, out IViewBinder binder)) { var unbindAction = binder.BindView(bindingExpression, bindingRegistration, converter); if (unbindAction != null) { _unbindActions.Add(unbindAction); } } else { if (Debugger.IsAttached) { Debugger.Break(); } } } } }
public BindingVisitor(VisualElement root, BindingRegistration bindingRegistration) : this(root) { m_Operation = bindingRegistration; }
internal static void UpdateSourceProperty <T>( BindingExpression bindingExpression, BindingRegistration bindingRegistration, T value, IValueConverter valueConverter, string converterParameter) { object newValue; var sourceProperty = bindingRegistration.GetSourceProperty(); if (sourceProperty == null) { return; } if (valueConverter != null) { newValue = valueConverter.ConvertBack(value, sourceProperty.PropertyInfo.PropertyType, converterParameter, CultureInfo.CurrentCulture); } else { // Implicit converter logic for DoubleToString round trip if (sourceProperty.PropertyInfo.PropertyType == typeof(double) && value is string) { double valueAsDouble; if (double.TryParse(value as string, out valueAsDouble)) { newValue = valueAsDouble; } else { newValue = default(double); } } // No implicit converter left else { newValue = value; } } try { // Must set through binding registration so that it'll correctly ignore property change events from just being set bindingRegistration.SetSourceValue(newValue, preObtainedSourceProperty: sourceProperty); } catch (Exception ex) { #if DEBUG if (Debugger.IsAttached) { Debugger.Break(); } #endif throw ex; } }