protected override void Dispose(bool isDisposing)
 {
     base.Dispose(isDisposing);
     if (isDisposing)
     {
         var view = View;
         if (view != null && _subscribed)
         {
             if (view is MvxEventSourceTabViewController)
             {
                 ((MvxEventSourceTabViewController)view).DidSelectCalled -= HandleValueChanged;
             }
             else
             {
                 try
                 {
                     view.TabView.DidSelect -= HandleValueChanged;
                 }
                 catch (Exception ex)
                 {
                     MvxBindingTrace.Error(ex.Message);
                 }
             }
             _subscribed = false;
         }
     }
 }
Esempio n. 2
0
        protected virtual void SetItemsSource(IEnumerable value)
        {
            if (_itemsSource == value)
            {
                return;
            }

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

            ReloadAllAnnotations();

            var newObservable = _itemsSource as INotifyCollectionChanged;

            if (newObservable != null)
            {
                _subscription = newObservable.WeakSubscribe(OnItemsSourceCollectionChanged);
            }
        }
Esempio n. 3
0
        public override void SetValue(object value)
        {
            var view = 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:
                MvxBindingTrace.Trace(MvxTraceLevel.Warning, "Visibility out of range {0}", value);
                break;
            }
        }
Esempio n. 4
0
        private void UpdateSourceFromTarget(object value)
        {
            if (value == MvxBindingConstant.DoNothing)
            {
                return;
            }

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

            try
            {
                this._sourceStep.SetValue(value);
            }
            catch (Exception exception)
            {
                MvxBindingTrace.Trace(
                    MvxTraceLevel.Error,
                    "Problem seen during binding execution for {0} - problem {1}",
                    this._bindingDescription.ToString(),
                    exception.ToLongString());
            }
        }
        public MvxGameObjectOnEventTargetBinding(GameObject gameObject, string eventName)
            : base(gameObject)
        {
            _eventName = eventName;
            if (gameObject == null)
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Error, "Error - GameObject is null in MvxGameObjectOnEventTargetBinding");
            }
            else
            {
                _eventTarget = UIEventListener.Get(gameObject);
                switch (_eventName)
                {
                case "onClick":
                    _eventTarget.onClick += this.OnClick;
                    break;

                case "onPress":
                    _eventTarget.onPress += this.OnPress;
                    break;

                case "onDrag":
                    _eventTarget.onDrag += this.OnDrag;
                    break;

                case "onDrop":
                    _eventTarget.onDrop += this.OnDrop;
                    break;

                case "onSelect":
                    _eventTarget.onSelect += this.OnSelect;
                    break;
                }
            }
        }
Esempio n. 6
0
        private object ApplyValueConverterSourceToTarget(object value)
        {
            if (this._description.Converter == null)
            {
                return(value);
            }

            try
            {
                return
                    (this._description.Converter.Convert(value,
                                                         this.TargetType,
                                                         this._description.ConverterParameter,
                                                         CultureInfo.CurrentUICulture));
            }
            catch (Exception exception)
            {
                // pokemon exception - force the use of Fallback in this case
                // we expect this exception to occur sometimes - so only "Diagnostic" level logging here
                MvxBindingTrace.Trace(
                    MvxTraceLevel.Diagnostic,
                    "Problem seen during binding execution for {0} - problem {1}",
                    this._description.ToString(),
                    exception.ToLongString());
            }

            return(MvxBindingConstant.UnsetValue);
        }
Esempio n. 7
0
        public MvxWithEventPropertyInfoTargetBinding(object target, PropertyInfo targetPropertyInfo)
            : base(target, targetPropertyInfo)
        {
            if (target == null)
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Error,
                                      "Error - target is null in MvxWithEventPropertyInfoTargetBinding");
                return;
            }

            var viewType  = target.GetType();
            var eventName = targetPropertyInfo.Name + "Changed";
            var eventInfo = viewType.GetEvent(eventName);

            if (eventInfo == null)
            {
                // this will be a one way binding
                return;
            }
            if (eventInfo.EventHandlerType != typeof(EventHandler))
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Diagnostic,
                                      "Diagnostic - cannot two-way bind to {0}/{1} on type {2} because eventHandler is type {3}",
                                      viewType,
                                      eventName,
                                      target.GetType().Name,
                                      eventInfo.EventHandlerType.Name);
                return;
            }

            _subscription = eventInfo.WeakSubscribe(target, OnValueChanged);
        }
        public override void SetValue(object value)
        {
            if (PropertyInfo == null)
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Warning, "SetValue ignored in binding - target property missing");
                return;
            }

            if (!PropertyInfo.CanWrite)
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Warning, "SetValue ignored in binding - target property is readonly");
                return;
            }

            try
            {
                if (PropertyInfo.PropertyType.IsGenericType && PropertyInfo.PropertyType.IsValueType)
                {
                    var underlyingType = Nullable.GetUnderlyingType(PropertyInfo.PropertyType);
                    PropertyInfo.SetValue(Source, Convert.ChangeType(value, underlyingType), null);
                }
                else
                {
                    PropertyInfo.SetValue(Source, value, null);
                }
            }
            catch (ThreadAbortException)
            {
                throw;
            }
            catch (Exception exception)
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Error, "SetValue failed with exception - " + exception.ToLongString());
            }
        }
        private static bool TryCreatePropertyDependencyBasedBinding(object target, string targetName,
                                                                    out IMvxTargetBinding binding)
        {
            if (target == null)
            {
                binding = null;
                return(false);
            }

            if (string.IsNullOrEmpty(targetName))
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Error,
                                      "Empty binding target passed to MvxWindowsTargetBindingFactoryRegistry");
                binding = null;
                return(false);
            }

            var dependencyProperty = target.GetType().FindDependencyProperty(targetName);

            if (dependencyProperty == null)
            {
                binding = null;
                return(false);
            }

            var actualProperty     = target.GetType().FindActualProperty(targetName);
            var actualPropertyType = actualProperty?.PropertyType ?? typeof(object);

            binding = new MvxDependencyPropertyTargetBinding(target, targetName, dependencyProperty, actualPropertyType);
            return(true);
        }
Esempio n. 10
0
        public override void SetValue(object value)
        {
            if (this.PropertyInfo == null)
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Warning,
                                      "SetValue ignored in binding - source property {0} is missing", this.PropertyName);
                return;
            }

            if (!this.PropertyInfo.CanWrite)
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Warning, "SetValue ignored in binding - target property is readonly");
                return;
            }

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

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

                this.PropertyInfo.SetValue(this.Source, safeValue, this.PropertyIndexParameters());
            }
            catch (Exception exception)
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Error,
                                      "SetValue failed with exception - " + exception.ToLongString());
            }
        }
Esempio n. 11
0
        private static void UpdateSourceFromTarget(MvxBindingRequest bindingRequest, IMvxSourceBinding sourceBinding, object value)
        {
            try
            {
                if (bindingRequest.Description.Converter != null)
                {
                    value =
                        bindingRequest.Description.Converter.ConvertBack(value,
                                                                         sourceBinding.SourceType,
                                                                         bindingRequest.Description.ConverterParameter,
                                                                         CultureInfo.CurrentUICulture);
                }
                sourceBinding.SetValue(value);
            }
            catch (ThreadAbortException)
            {
                throw;
            }
            catch (Exception exception)
            {
                MvxBindingTrace.Trace(
                    MvxTraceLevel.Error,

                    "Problem seen during binding execution for {0} - problem {1}",
                    bindingRequest.ToString(),
                    exception.ToLongString());
            }
        }
Esempio n. 12
0
        private void RemoveHandler()
        {
            switch (_controlEvent)
            {
            case MvxIosPropertyBinding.UIControl_TouchDown:
            case MvxIosPropertyBinding.UIControl_TouchDownRepeat:
            case MvxIosPropertyBinding.UIControl_TouchDragInside:
            case MvxIosPropertyBinding.UIControl_TouchUpInside:
            case MvxIosPropertyBinding.UIControl_ValueChanged:
            case MvxIosPropertyBinding.UIControl_PrimaryActionTriggered:
            case MvxIosPropertyBinding.UIControl_EditingDidBegin:
            case MvxIosPropertyBinding.UIControl_EditingChanged:
            case MvxIosPropertyBinding.UIControl_EditingDidEnd:
            case MvxIosPropertyBinding.UIControl_EditingDidEndOnExit:
            case MvxIosPropertyBinding.UIControl_AllTouchEvents:
            case MvxIosPropertyBinding.UIControl_AllEditingEvents:
            case MvxIosPropertyBinding.UIControl_AllEvents:
                _controlEventSubscription?.Dispose();
                break;

            default:
                MvxBindingTrace.Trace(MvxTraceLevel.Error, "Error - Invalid controlEvent in MvxUIControlTargetBinding");
                break;
            }
        }
        public override void SubscribeToEvents()
        {
            var target = this.Target;

            if (target == null)
            {
                return;
            }

            var viewType  = target.GetType();
            var eventName = this.TargetPropertyInfo.Name + "Changed";
            var eventInfo = viewType.GetEvent(eventName);

            if (eventInfo == null)
            {
                // this will be a one way binding
                return;
            }

            if (eventInfo.EventHandlerType != typeof(EventHandler))
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Diagnostic,
                                      "Diagnostic - cannot two-way bind to {0}/{1} on type {2} because eventHandler is type {3}",
                                      viewType,
                                      eventName,
                                      target.GetType().Name,
                                      eventInfo.EventHandlerType.Name);
                return;
            }

            this._subscription = eventInfo.WeakSubscribe(target, OnValueChanged);
        }
Esempio n. 14
0
        public override void SetValue(object value)
        {
            if (PropertyInfo == null)
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Warning, "SetValue ignored in binding - target property missing");
                return;
            }

            if (!PropertyInfo.CanWrite)
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Warning, "SetValue ignored in binding - target property is readonly");
                return;
            }

            try
            {
                PropertyInfo.SetValue(Source, value, null);
            }
            catch (ThreadAbortException)
            {
                throw;
            }
            catch (Exception exception)
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Error, "SetValue failed with exception - " + exception.ToLongString());
            }
        }
Esempio n. 15
0
        private void BindView(View view, Context context, IAttributeSet attrs)
        {
            var typedArray = context.ObtainStyledAttributes(attrs, MvxAndroidBindingResource.Instance.BindingStylableGroupId);

            int numStyles = typedArray.IndexCount;

            for (var i = 0; i < numStyles; ++i)
            {
                var attributeId = typedArray.GetIndex(i);

                if (attributeId == MvxAndroidBindingResource.Instance.BindingBindId)
                {
                    try
                    {
                        var bindingText = typedArray.GetString(attributeId);
                        var newBindings = this.GetService <IMvxBinder>().Bind(_source, view, bindingText);
                        if (newBindings != null)
                        {
                            var asList = newBindings.ToList();
                            _viewBindings[view] = asList;
                        }
                    }
                    catch (Exception exception)
                    {
                        MvxBindingTrace.Trace(MvxTraceLevel.Error, "Exception thrown during the view binding {0}", exception.ToLongString());
                        throw;
                    }
                }
            }
            typedArray.Recycle();
        }
Esempio n. 16
0
 public DecimalEditTextTargetBinding(EditText target) : base(target)
 {
     if (target == null)
     {
         MvxBindingTrace.Error($"Error - EditText is null in {nameof(DecimalEditTextTargetBinding)}");
     }
 }
Esempio n. 17
0
 private void UpdateTargetFromSource(
     bool isAvailable,
     object value)
 {
     try
     {
         if (isAvailable)
         {
             if (_bindingDescription.Converter != null)
             {
                 value =
                     _bindingDescription.Converter.Convert(value,
                                                           _targetBinding.TargetType,
                                                           _bindingDescription.ConverterParameter,
                                                           CultureInfo.CurrentUICulture);
             }
         }
         else
         {
             value = _bindingDescription.FallbackValue;
         }
         _targetBinding.SetValue(value);
     }
     catch (Exception exception)
     {
         MvxBindingTrace.Trace(
             MvxTraceLevel.Error,
             "Problem seen during binding execution for {0} - problem {1}",
             _bindingDescription.ToString(),
             exception.ToLongString());
     }
 }
        public virtual void FillFrom(IMvxNamedInstanceRegistry <T> registry, Assembly assembly)
        {
            var pairs = from type in assembly.ExceptionSafeGetTypes()
                        where type.GetTypeInfo().IsPublic
                        where !type.GetTypeInfo().IsAbstract
                        where typeof(T).IsAssignableFrom(type)
                        let name = this.FindName(type)
                                   where !string.IsNullOrEmpty(name)
                                   where type.IsConventional()
                                   select new { Name = name, Type = type };

            foreach (var pair in pairs)
            {
                try
                {
                    var converter = Activator.CreateInstance(pair.Type) as T;
                    MvxBindingTrace.Trace("Registering value converter {0}:{1}", pair.Name, pair.Type.Name);
                    registry.AddOrOverwrite(pair.Name, converter);
                }
                catch (Exception)
                {
                    // ignore this
                }
            }
        }
Esempio n. 19
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)
            {
                MvxBindingTrace.Trace(MvxTraceLevel.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);
        }
Esempio n. 20
0
        protected override bool GetBitmap(object value, out Bitmap bitmap)
        {
            if (!(value is int))
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Warning,
                                      "Value was not a valid Drawable");
                bitmap = null;
                return(false);
            }

            var intValue = (int)value;

            if (intValue == 0)
            {
                bitmap = null;
            }
            else
            {
                var resources = this.AndroidGlobals.ApplicationContext.Resources;
                bitmap = BitmapFactory.DecodeResource(resources, intValue, new BitmapFactory.Options()
                {
                    InPurgeable = true
                });
            }

            return(true);
        }
Esempio n. 21
0
        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)
            {
                MvxBindingTrace.Trace(
                    MvxTraceLevel.Warning,
                    "Unable to bind: source property source not found {0} on {1}"
                    , currentToken
                    , source.GetType().Name);
            }

            return(new MvxMissingSourceBinding(source));
        }
        protected MvxBasePropertyInfoSourceBinding(object source, string propertyName)
            : base(source)
        {
            _propertyName = propertyName;

            if (Source == null)
            {
                MvxBindingTrace.Trace(
                    MvxTraceLevel.Warning,
                    "Unable to bind to source is null"
                    , propertyName);
                return;
            }

            _propertyInfo = source.GetType().GetProperty(propertyName);
            if (_propertyInfo == null)
            {
                MvxBindingTrace.Trace(
                    MvxTraceLevel.Warning,
                    "Unable to bind: source property source not found {0} on {1}"
                    , propertyName,
                    source.GetType().Name);
            }

            var sourceNotify = Source as INotifyPropertyChanged;

            if (sourceNotify != null)
            {
                sourceNotify.PropertyChanged += new PropertyChangedEventHandler(SourcePropertyChanged);
            }
        }
        protected override void SetValueImpl(object target, object value)
        {
            var imageView = (ImageView)target;

            if (!(value is string))
            {
                MvxBindingTrace.Trace(MvxTraceLevel.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)
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Warning,
                                      "Value '{0}' was not a known drawable name", value);
                imageView.SetImageDrawable(null);
                return;
            }

            base.SetValueImpl(target, id);
        }
Esempio n. 24
0
        private void UpdateTargetFromSource(
            object value)
        {
            if (value == MvxBindingConstant.DoNothing)
            {
                return;
            }

            if (value == MvxBindingConstant.UnsetValue)
            {
                value = _targetBinding.TargetType.CreateDefault();
            }

            try
            {
                _targetBinding.SetValue(value);
            }
            catch (Exception exception)
            {
                MvxBindingTrace.Trace(
                    MvxTraceLevel.Error,
                    "Problem seen during binding execution for {0} - problem {1}",
                    _bindingDescription.ToString(),
                    exception.ToLongString());
            }
        }
Esempio n. 25
0
        private static bool TryGetPropertyValue(this object host, string targetPropertyName, out object value)
        {
            if (host == null)
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Warning, "Unable to bind to null host object - property {0}",
                                      targetPropertyName);
                value = null;
                return(false);
            }

            var propertyInfo = host.GetType().GetProperty(targetPropertyName);

            if (propertyInfo == null)
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Warning, "Unable to find property for binding - property {0}",
                                      targetPropertyName);
                value = null;
                return(false);
            }
            value = propertyInfo.GetValue(host, null);
            if (value == null)
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Warning, "property for binding is null - property {0}",
                                      targetPropertyName);
                return(false);
            }

            return(true);
        }
        protected MvxPropertyInfoSourceBinding(object source, string propertyName)
            : base(source)
        {
            _propertyName = propertyName;

            if (Source == null)
            {
                MvxBindingTrace.Trace(
                    // this is not a Warning - as actually using a NULL source is a fairly common occurrence!
                    MvxTraceLevel.Diagnostic,
                    "Unable to bind to source as it's null"
                    , propertyName);
                return;
            }

            _propertyInfo = source.GetType().GetProperty(propertyName);
            if (_propertyInfo == null)
            {
                MvxBindingTrace.Trace(
                    MvxTraceLevel.Warning,
                    "Unable to bind: source property source not found {0} on {1}"
                    , propertyName,
                    source.GetType().Name);
            }

            var sourceNotify = Source as INotifyPropertyChanged;

            if (sourceNotify != null)
            {
                _subscription = sourceNotify.WeakSubscribe(SourcePropertyChanged);
            }
        }
Esempio n. 27
0
        public bool TryParseBindingSpecification(string text, out MvxSerializableBindingSpecification requestedBindings)
        {
            try
            {
                Reset(text);

                var toReturn = new MvxSerializableBindingSpecification();
                while (!IsComplete)
                {
                    SkipWhitespaceAndDescriptionSeparators();
                    var result = ParseTargetPropertyNameAndDescription();
                    toReturn[result.Key] = result.Value;
                    SkipWhitespaceAndDescriptionSeparators();
                }

                requestedBindings = toReturn;
                return(true);
            }
            catch (Exception exception)
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Error,
                                      "Problem parsing binding {0}", exception.ToLongString());
                requestedBindings = null;
                return(false);
            }
        }
Esempio n. 28
0
        private View CreateView(string name, Context context, IAttributeSet attrs)
        {
            // resolve the tag name to a type
            var viewType = ViewTypeResolver.Resolve(name);

            if (viewType == null)
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Error, "View type not found - {0}", name);
                return(null);
            }

            try
            {
                var view = Activator.CreateInstance(viewType, context, attrs) as View;
                if (view == null)
                {
                    MvxBindingTrace.Trace(MvxTraceLevel.Error, "Unable to load view {0} from type {1}", name, viewType.FullName);
                }
                return(view);
            }
            catch (ThreadAbortException)
            {
                throw;
            }
            catch (Exception exception)
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Error, "Exception during creation of {0} from type {1} - exception {2}", name, viewType.FullName, exception.ToLongString());
                return(null);
            }
        }
        protected virtual void SetItemsSource(IEnumerable value)
        {
            if (ReferenceEquals(_itemsSource, value) && !ReloadOnAllItemsSourceSets)
            {
                return;
            }

            _subscription?.Dispose();
            _subscription = null;

            _itemsSource = value;

            if (_itemsSource != null && !(_itemsSource is IList))
            {
                MvxBindingTrace.Trace(MvxTraceLevel.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 override void SubscribeToEvents()
        {
            var view = View;

            if (view == null)
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Error, "Error - NSTabViewController is null in MvxNSTabViewControllerSelectedTabViewItemIndexTargetBinding");
                return;
            }

            _subscribed = true;
            if (view is MvxEventSourceTabViewController)
            {
                ((MvxEventSourceTabViewController)view).DidSelectCalled += HandleValueChanged;
            }
            else
            {
                try {
                    view.TabView.DidSelect += HandleValueChanged;
                }
                catch (Exception ex)
                {
                    MvxBindingTrace.Error(ex.Message);
                }
            }
        }