protected virtual void SetItemsSource(IEnumerable value)
        {
            if (ReferenceEquals(_itemsSource, value) && !ReloadOnAllItemsSourceSets)
            {
                return;
            }

            _subscription?.Dispose();
            _subscription = null;

            _itemsSource = value;

            if (_itemsSource != null && !(_itemsSource is IList))
            {
                MvxBindingLog.Warning("Binding to IEnumerable rather than IList - this can be inefficient, especially for large lists");
            }

            var newObservable = _itemsSource as INotifyCollectionChanged;

            if (newObservable != null)
            {
                _subscription = newObservable.WeakSubscribe(OnItemsSourceCollectionChanged);
            }

            NotifyDataSetChanged();
        }
        public IMvxSourceBinding CreateBinding(object source, IList <MvxPropertyToken> tokens)
        {
            if (tokens == null || tokens.Count == 0)
            {
                throw new MvxException("empty token list passed to CreateBinding");
            }

            var currentToken    = tokens[0];
            var remainingTokens = tokens.Skip(1).ToList();
            IMvxSourceBinding extensionResult;

            if (TryCreateBindingFromExtensions(source, currentToken, remainingTokens, out extensionResult))
            {
                return(extensionResult);
            }

            if (source != null)
            {
                MvxBindingLog.Warning(
                    "Unable to bind: source property source not found {0} on {1}"
                    , currentToken
                    , source.GetType().Name);
            }

            return(new MvxMissingSourceBinding(source));
        }
Beispiel #3
0
        protected override void SetValueImpl(object target, object value)
        {
            var listView = (MvxExpandableListView)target;

            if (value == null)
            {
                _currentValue = null;
                listView.ClearChoices();
                return;
            }
            var positions = ((MvxExpandableListAdapter)listView.ExpandableListAdapter).GetPositions(value);

            if (positions == null)
            {
                MvxBindingLog.Warning("Value not found for spinner {0}", value.ToString());
                return;
            }

            _currentValue = value;
            listView.SetSelectedChild(positions.Item1, positions.Item2, true);

            var pos =
                listView.GetFlatListPosition(ExpandableListView.GetPackedPositionForChild(positions.Item1,
                                                                                          positions.Item2));

            listView.SetItemChecked(pos, true);
        }
        public override void SetValue(object value)
        {
            if (PropertyInfo == null)
            {
                MvxBindingLog.Warning("SetValue ignored in binding - source property {0} is missing", PropertyName);
                return;
            }

            if (!PropertyInfo.CanWrite)
            {
                MvxBindingLog.Warning("SetValue ignored in binding - target property {0}.{1} is readonly", PropertyInfo.DeclaringType?.Name, PropertyName);
                return;
            }

            try
            {
                var propertyType = PropertyInfo.PropertyType;
                var safeValue    = propertyType.MakeSafeValue(value);

                // if safeValue matches the existing value, then don't call set
                if (EqualsCurrentValue(safeValue))
                {
                    return;
                }

                PropertyInfo.SetValue(Source, safeValue, PropertyIndexParameters());
            }
            catch (Exception exception)
            {
                MvxBindingLog.Error("SetValue failed with exception - " + exception.ToLongString());
            }
        }
Beispiel #5
0
        protected override void SetValueImpl(object target, object value)
        {
            var view = this.View;

            if (view == null)
            {
                return;
            }

            var visibility = (MvxVisibility)value;

            switch (visibility)
            {
            case MvxVisibility.Visible:
                view.Hidden = false;
                break;

            case MvxVisibility.Collapsed:
                view.Hidden = true;
                break;

            default:
                MvxBindingLog.Warning("Visibility out of range {0}", value);
                break;
            }
        }
Beispiel #6
0
        protected override void SetValueImpl(object target, object value)
        {
            var imageView = (ImageView)target;

            if (!(value is string))
            {
                MvxBindingLog.Warning(
                    "Value '{0}' could not be parsed as a valid string identifier", value);
                imageView.SetImageDrawable(null);
                return;
            }

            var resources = AndroidGlobals.ApplicationContext.Resources;
            var id        = resources.GetIdentifier((string)value, "drawable", AndroidGlobals.ApplicationContext.PackageName);

            if (id == 0)
            {
                MvxBindingLog.Warning(
                    "Value '{0}' was not a known drawable name", value);
                imageView.SetImageDrawable(null);
                return;
            }

            base.SetValueImpl(target, id);
        }
Beispiel #7
0
 private void ParseFallbackValue(string block, MvxSerializableBindingDescription description)
 {
     ParseEquals(block);
     if (description.FallbackValue != null)
     {
         MvxBindingLog.Warning("Overwriting existing FallbackValue");
     }
     description.FallbackValue = ReadValue();
 }
Beispiel #8
0
 private void ParseConverterParameter(string block, MvxSerializableBindingDescription description)
 {
     ParseEquals(block);
     if (description.ConverterParameter != null)
     {
         MvxBindingLog.Warning("Overwriting existing ConverterParameter");
     }
     description.ConverterParameter = ReadValue();
 }
        public override void SetValue(object value)
        {
            if (_currentChildBinding == null)
            {
                MvxBindingLog.Warning("SetValue ignored in binding - target property path missing");
                return;
            }

            _currentChildBinding.SetValue(value);
        }
Beispiel #10
0
        private void ParseConverter(string block, MvxSerializableBindingDescription description)
        {
            ParseEquals(block);
            var converter = ReadTargetPropertyName();

            if (!string.IsNullOrEmpty(description.Converter))
            {
                MvxBindingLog.Warning("Overwriting existing Converter with {0}", converter);
            }
            description.Converter = converter;
        }
        public static bool TargetIsInvalid(object target)
        {
            var javaTarget = target as IJavaObject;

            if (javaTarget != null && javaTarget.Handle == IntPtr.Zero)
            {
                MvxBindingLog.Warning("Weak Target has been GCed by Android {0}", javaTarget.GetType().Name);
                return(true);
            }
            return(false);
        }
Beispiel #12
0
        protected virtual object GetValueByReflection()
        {
            var target = Target as BindableObject;

            if (target == null)
            {
                MvxBindingLog.Warning("Weak Target is null in {0} - skipping Get", GetType().Name);
                return(null);
            }

            return(target.GetValue(_targetBindableProperty));
        }
        protected virtual object GetValueByReflection()
        {
            var target = Target as FrameworkElement;

            if (target == null)
            {
                MvxBindingLog.Warning("Weak Target is null in {0} - skipping Get", GetType().Name);
                return(null);
            }

            return(target.GetValue(_targetDependencyProperty));
        }
Beispiel #14
0
        public override void SetValue(object value)
        {
            MvxBindingLog.Trace("Receiving SetValue to " + (value ?? ""));
            var target = Target;

            if (target == null)
            {
                MvxBindingLog.Warning("Weak Target is null in {0} - skipping set", GetType().Name);
                return;
            }

            if (ShouldSkipSetValueForPlatformSpecificReasons(target, value))
            {
                return;
            }

            if (ShouldSkipSetValueForViewSpecificReasons(target, value))
            {
                return;
            }

            var safeValue = MakeSafeValue(value);

            // to prevent feedback loops, we don't pass on 'same value' updates from the source while we are updating it
            if (_isUpdatingSource)
            {
                if (safeValue == null)
                {
                    if (_updatingSourceWith == null)
                    {
                        return;
                    }
                }
                else
                {
                    if (safeValue.Equals(_updatingSourceWith))
                    {
                        return;
                    }
                }
            }

            try
            {
                _isUpdatingTarget = true;
                SetValueImpl(target, safeValue);
            }
            finally
            {
                _isUpdatingTarget = false;
            }
        }
        public MvxFluentBindingDescription <TTarget, TSource> FullyDescribed(string bindingDescription)
        {
            var newBindingDescription =
                MvxBindingSingletonCache.Instance.BindingDescriptionParser.Parse(bindingDescription)
                .ToList();

            if (newBindingDescription.Count > 1)
            {
                MvxBindingLog.Warning("More than one description found - only first will be used in {0}", bindingDescription);
            }

            return(FullyDescribed(newBindingDescription.FirstOrDefault()));
        }
Beispiel #16
0
        protected override bool GetBitmap(object value, out Bitmap bitmap)
        {
            if (!(value is Bitmap))
            {
                MvxBindingLog.Warning(
                    "Value was not a valid Bitmap");
                bitmap = null;
                return(false);
            }

            bitmap = (Bitmap)value;
            return(true);
        }
        protected override void SetValueImpl(object target, object value)
        {
            MvxBindingLog.Trace("Receiving setValue to " + (value ?? ""));
            var frameworkElement = target as FrameworkElement;

            if (frameworkElement == null)
            {
                MvxBindingLog.Warning("Weak Target is null in {0} - skipping set", GetType().Name);
                return;
            }

            frameworkElement.SetValue(_targetDependencyProperty, value);
        }
        private IMvxTargetBinding CreateTargetBinding(object target, PropertyInfo targetPropertyInfo)
        {
            var targetBindingCandidate = Activator.CreateInstance(_bindingType, target, targetPropertyInfo);
            var targetBinding          = targetBindingCandidate as IMvxTargetBinding;

            if (targetBinding == null)
            {
                MvxBindingLog.Warning("The TargetBinding created did not support IMvxTargetBinding");
                var disposable = targetBindingCandidate as IDisposable;
                disposable?.Dispose();
            }
            return(targetBinding);
        }
Beispiel #19
0
        protected override void SetValueImpl(object target, object value)
        {
            MvxBindingLog.Trace("Receiving setValue to " + (value ?? ""));
            var bindableObject = target as BindableObject;

            if (bindableObject == null)
            {
                MvxBindingLog.Warning("Weak Target is null in {0} - skipping set", GetType().Name);
                return;
            }

            bindableObject.SetValue(_targetBindableProperty, value);
        }
Beispiel #20
0
        public override bool TryGetValue(IEnumerable <IMvxSourceStep> steps, out object value)
        {
            var list = steps.ToList();

            switch (list.Count)
            {
            case 2:
                return(TryEvaluateif(list[0], list[1], null, out value));

            case 3:
                return(TryEvaluateif(list[0], list[1], list[2], out value));

            default:
                MvxBindingLog.Warning("Unexpected substep count of {0} in 'If' ValueCombiner", list.Count);
                return(base.TryGetValue(list, out value));
            }
        }
Beispiel #21
0
        public override bool TryGetValue(IEnumerable <IMvxSourceStep> steps, out object value)
        {
            var list = steps.ToList();

            if (list.Count < 1)
            {
                MvxBindingLog.Warning("Format called with no parameters - will fail");
                value = MvxBindingConstant.DoNothing;
                return(true);
            }

            var formatObject = list.First().GetValue();

            if (formatObject == MvxBindingConstant.DoNothing)
            {
                value = MvxBindingConstant.DoNothing;
                return(true);
            }

            if (formatObject == MvxBindingConstant.UnsetValue)
            {
                value = MvxBindingConstant.UnsetValue;
                return(true);
            }

            var formatString = formatObject == null ? "" : formatObject.ToString();

            var values = list.Skip(1).Select(s => s.GetValue()).ToArray();

            if (values.Any(v => v == MvxBindingConstant.DoNothing))
            {
                value = MvxBindingConstant.DoNothing;
                return(true);
            }

            if (values.Any(v => v == MvxBindingConstant.UnsetValue))
            {
                value = MvxBindingConstant.UnsetValue;
                return(true);
            }

            value = string.Format(formatString, values);
            return(true);
        }
Beispiel #22
0
        private void CreateTargetBinding(object target)
        {
            _targetBinding = TargetBindingFactory.CreateBinding(target, _bindingDescription.TargetName);

            if (_targetBinding == null)
            {
                MvxBindingLog.Warning("Failed to create target binding for {0}", _bindingDescription.ToString());
                _targetBinding = new MvxNullTargetBinding();
            }

            if (NeedToObserveTargetChanges)
            {
                _targetBinding.SubscribeToEvents();
                _targetBindingOnValueChanged = (sender, args) => UpdateSourceFromTarget(args.Value);
                _targetBinding.ValueChanged += _targetBindingOnValueChanged;
            }

            _defaultTargetValue = _targetBinding.TargetType.CreateDefault();
        }
        protected override void SetValueImpl(object target, object value)
        {
            if (value == null || value == _currentValue)
            {
                return;
            }

            var listView = (MvxListView)target;

            var index = listView.Adapter.GetPosition(value);

            if (index < 0)
            {
                MvxBindingLog.Warning("Value not found for spinner {0}", value.ToString());
                return;
            }
            _currentValue = value;
            listView.SetSelection(index);
        }
Beispiel #24
0
 private void ParseCommandParameter(string block, MvxSerializableBindingDescription description)
 {
     if (!IsComplete &&
         CurrentChar == '(')
     {
         // following https://github.com/MvvmCross/MvvmCross/issues/704, if the next character is "(" then
         // we can treat CommandParameter as a normal non-keyword block
         ParseNonKeywordBlockInto(description, block);
     }
     else
     {
         ParseEquals(block);
         if (!string.IsNullOrEmpty(description.Converter))
         {
             MvxBindingLog.Warning("Overwriting existing Converter with CommandParameter");
         }
         description.Converter          = "CommandParameter";
         description.ConverterParameter = ReadValue();
     }
 }
        public void ApplyWithClearBindingKey(object clearBindingKey)
        {
            foreach (var applicable in _applicables)
            {
                if (applicable is IMvxBaseFluentBindingDescription fluentBindingDescription)
                {
                    fluentBindingDescription.ClearBindingKey = clearBindingKey;
                }
                else
                {
                    MvxBindingLog.Warning("Fluent binding description must implement {0} in order to add {1}",
                                          nameof(IMvxBaseFluentBindingDescription),
                                          nameof(IMvxBaseFluentBindingDescription.ClearBindingKey));
                }

                applicable.Apply();
            }

            base.Apply();
        }
        public static bool RequireTargetUpdateOnFirstBind(this MvxBindingMode bindingMode)
        {
            switch (bindingMode)
            {
            case MvxBindingMode.Default:
                MvxBindingLog.Warning("Mode of default seen for binding - assuming TwoWay");
                return(true);

            case MvxBindingMode.OneWay:
            case MvxBindingMode.OneTime:
            case MvxBindingMode.TwoWay:
                return(true);

            case MvxBindingMode.OneWayToSource:
                return(false);

            default:
                throw new MvxException("Unexpected ActualBindingMode");
            }
        }
        protected override Stream GetStream(object value)
        {
            if (value == null)
            {
                MvxBindingLog.Warning("Null value passed to ImageView binding");
                return(null);
            }

            var stringValue = value as string;

            if (string.IsNullOrWhiteSpace(stringValue))
            {
                MvxBindingLog.Warning("Empty value passed to ImageView binding");
                return(null);
            }

            var drawableResourceName = GetImageAssetName(stringValue);
            var assetStream          = AndroidGlobals.ApplicationContext.Assets.Open(drawableResourceName);

            return(assetStream);
        }
Beispiel #28
0
        public bool TryCreateBinding(object source, MvxPropertyToken currentToken, List <MvxPropertyToken> remainingTokens, out IMvxSourceBinding result)
        {
            if (source == null)
            {
                result = null;
                return(false);
            }

            if (remainingTokens.Count > 0)
            {
                result = null;
                return(false);
            }

            var propertyNameToken = currentToken as MvxPropertyNamePropertyToken;

            if (propertyNameToken == null)
            {
                result = null;
                return(false);
            }

            var methodInfo = FindMethodInfo(source, propertyNameToken.PropertyName);

            if (methodInfo == null)
            {
                result = null;
                return(false);
            }

            var parameters = methodInfo.GetParameters();

            if (parameters.Count(p => !p.IsOptional) > 1)
            {
                MvxBindingLog.Warning("Problem binding to Method {0} - too many non-optional parameters");
            }

            result = new MvxMethodSourceBinding(source, methodInfo);
            return(true);
        }
        protected override void SetValueImpl(object target, object value)
        {
            var spinner = (MvxSpinner)target;

            if (value == null)
            {
                MvxBindingLog.Warning("Null values not permitted in spinner SelectedItem binding currently");
                return;
            }

            if (!value.Equals(_currentValue))
            {
                var index = spinner.Adapter.GetPosition(value);
                if (index < 0)
                {
                    MvxBindingLog.Warning("Value not found for spinner {0}", value.ToString());
                    return;
                }
                _currentValue = value;
                spinner.SetSelection(index);
            }
        }
Beispiel #30
0
        protected override void SetValueImpl(object target, object value)
        {
            var imageView = (ImageView)target;

            if (!(value is int))
            {
                MvxBindingLog.Warning(
                    "Value was not a valid Drawable");
                imageView.SetImageDrawable(null);
                return;
            }

            var intValue = (int)value;

            if (intValue == 0)
            {
                imageView.SetImageDrawable(null);
            }
            else
            {
                SetImage(imageView, intValue);
            }
        }