Esempio n. 1
0
        /// <summary>
        ///     Applies the binding expression to a previously set source or target.
        /// </summary>
        void ApplyCore(object sourceObject, BindableObject target, BindableProperty property, bool fromTarget = false)
        {
            BindingMode mode = Binding.GetRealizedMode(_targetProperty);

            if ((mode == BindingMode.OneWay || mode == BindingMode.OneTime) && fromTarget)
            {
                return;
            }

            bool needsGetter = (mode == BindingMode.TwoWay && !fromTarget) || mode == BindingMode.OneWay || mode == BindingMode.OneTime;
            bool needsSetter = !needsGetter && ((mode == BindingMode.TwoWay && fromTarget) || mode == BindingMode.OneWayToSource);

            object current             = sourceObject;
            BindingExpressionPart part = null;

            for (var i = 0; i < _parts.Count; i++)
            {
                part = _parts[i];

                if (!part.IsSelf && current != null)
                {
                    // Allow the object instance itself to provide its own TypeInfo
                    TypeInfo currentType = current is IReflectableType reflectable?reflectable.GetTypeInfo() : current.GetType().GetTypeInfo();

                    if (part.LastGetter == null || !part.LastGetter.DeclaringType.GetTypeInfo().IsAssignableFrom(currentType))
                    {
                        SetupPart(currentType, part);
                    }

                    if (i < _parts.Count - 1)
                    {
                        part.TryGetValue(current, out current);
                    }
                }

                if (!part.IsSelf &&
                    current != null &&
                    ((needsGetter && part.LastGetter == null) ||
                     (needsSetter && part.NextPart == null && part.LastSetter == null)))
                {
                    Log.Warning("Binding", PropertyNotFoundErrorMessage, part.Content, current, target.GetType(), property.PropertyName);
                    break;
                }

                if (part.NextPart != null && (mode == BindingMode.OneWay || mode == BindingMode.TwoWay) &&
                    current is INotifyPropertyChanged inpc)
                {
                    part.Subscribe(inpc);
                }
            }

            Debug.Assert(part != null, "There should always be at least the self part in the expression.");

            if (needsGetter)
            {
                if (part.TryGetValue(current, out object value) || part.IsSelf)
                {
                    value = Binding.GetSourceValue(value, property.ReturnType);
                }
                else
                {
                    value = Binding.FallbackValue ?? property.GetDefaultValue(target);
                }

                if (!TryConvert(ref value, property, property.ReturnType, true))
                {
                    Log.Warning("Binding", "'{0}' can not be converted to type '{1}'.", value, property.ReturnType);
                    return;
                }

                target.SetValueCore(property, value, SetValueFlags.ClearDynamicResource, BindableObject.SetValuePrivateFlags.Default | BindableObject.SetValuePrivateFlags.Converted);
            }
            else if (needsSetter && part.LastSetter != null && current != null)
            {
                object value = Binding.GetTargetValue(target.GetValue(property), part.SetterType);

                if (!TryConvert(ref value, property, part.SetterType, false))
                {
                    Log.Warning("Binding", "'{0}' can not be converted to type '{1}'.", value, part.SetterType);
                    return;
                }

                object[] args;
                if (part.IsIndexer)
                {
                    args = new object[part.Arguments.Length + 1];
                    part.Arguments.CopyTo(args, 0);
                    args[args.Length - 1] = value;
                }
                else if (part.IsBindablePropertySetter)
                {
                    args = new[] { part.BindablePropertyField, value };
                }
                else
                {
                    args = new[] { value };
                }

                part.LastSetter.Invoke(current, args);
            }
        }
Esempio n. 2
0
        private static DependencyProperty FindMessageProperty(DependencyObject e)
        {
            DependencyProperty property;

            if (FindMessageDependencyProperty != null)
            {
                property = FindMessageDependencyProperty(e);
                if (property != null)
                {
                    return(property);
                }
            }

            if (MessageDependencyPropertyMap.TryGetValue(e.GetType(), out property))
            {
                return(property);
            }
#if !XAMARINFORMS
            if (e is TextBlock)
            {
                return(TextBlock.TextProperty);

#if WINDOWS_PHONE
            }
            else if (e is PanoramaItem)
            {
                return(PanoramaItem.HeaderProperty);
            }
            else if (e is Pivot)
            {
                return(Pivot.TitleProperty);
            }
            else if (e is PivotItem)
            {
                return(PivotItem.HeaderProperty);
            }
            else if (e is ListPicker)
            {
                return(ListPicker.HeaderProperty);
#endif
#if SILVERLIGHT
            }
            else if (e is TabItem)
            {
                return(TabItem.HeaderProperty);
#endif
            }
            else if (e is ContentControl)
            {
                return(ContentControl.ContentProperty);
            }
            else if (e is TextBox)
            {
                return(TextBox.TextProperty);
            }
#else
            if (e is Label)
            {
                return(Label.TextProperty);
            }
            else if (e is Entry)
            {
                return(Entry.TextProperty);
            }
            else if (e is Page)
            {
                return(Page.TitleProperty);
            }
            else if (e is Button)
            {
                return(Button.TextProperty);
            }
            else if (e is TextCell)
            {
                return(TextCell.TextProperty);
            }
            else if (e is EntryCell)
            {
                return(EntryCell.LabelProperty);
            }
            else if (e is SwitchCell)
            {
                return(SwitchCell.TextProperty);
            }
#endif

            return(null);
        }
Esempio n. 3
0
 static object CreateDefaultRoute(BindableObject bindable)
 {
     return(bindable.GetType().Name + ++s_routeCount);
 }