Beispiel #1
0
    /// <summary>
    /// Binds a property to a view, this is the standard property binding extension method.
    /// </summary>
    /// <typeparam name="TBindingType"></typeparam>
    /// <param name="bindable"></param>
    /// <param name="sourceProperty"></param>
    /// <param name="targetSetter"></param>
    /// <param name="onlyWhenChanged"></param>
    /// <returns></returns>
    public static IDisposable BindProperty <TBindingType>(this IBindable bindable, P <TBindingType> sourceProperty, Action <TBindingType> targetSetter, bool onlyWhenChanged = true)
    {
        targetSetter(sourceProperty.Value);
        if (onlyWhenChanged)
        {
            return(bindable.AddBinding(sourceProperty.Where(p => sourceProperty.LastValue != sourceProperty.ObjectValue).Subscribe(targetSetter)));
        }

        return(bindable.AddBinding(sourceProperty.Subscribe(targetSetter)));
    }
Beispiel #2
0
        /// <summary>
        /// Binds a property to a view, this is the standard property binding extension method.
        /// </summary>
        /// <typeparam name="TBindingType"></typeparam>
        /// <param name="property"></param>
        /// <param name="bindable"></param>
        /// <param name="changed"></param>
        /// <param name="onlyWhenChanged"></param>
        /// <returns></returns>
        public static IDisposable BindTwoWay <TBindingType>(this IBindable bindable, P <TBindingType> property,
                                                            Action <TBindingType> changed, bool onlyWhenChanged = true)
        {
            changed(property.Value);
            if (onlyWhenChanged)
            {
                return
                    (bindable.AddBinding(
                         property.Where(p => !P <TBindingType> .EqualityComparer.Equals(property.Value, property.LastValue)).ObserveOnMainThread().Subscribe(changed)));//uFrame_kbe:ObserveOnMainThread()
            }

            return(bindable.AddBinding(property.ObserveOnMainThread().Subscribe(changed)));//uFrame_kbe:ObserveOnMainThread()
        }
Beispiel #3
0
        /// <summary>
        /// Binds a property to a view, this is the standard property binding extension method.
        /// </summary>
        /// <typeparam name="TBindingType"></typeparam>
        /// <param name="property"></param>
        /// <param name="bindable"></param>
        /// <param name="changed"></param>
        /// <param name="onlyWhenChanged"></param>
        /// <returns></returns>
        public static IDisposable BindTwoWay <TBindingType>(this IBindable bindable, P <TBindingType> property,
                                                            Action <TBindingType> changed, bool onlyWhenChanged = true)
        {
            changed(property.Value);
            if (onlyWhenChanged)
            {
                return
                    (bindable.AddBinding(
                         property.Where(p => property.LastValue != property.ObjectValue).Subscribe(changed)));
            }

            return(bindable.AddBinding(property.Subscribe(changed)));
        }
Beispiel #4
0
        /// <summary>
        /// Binds a property to a view, this is the standard property binding extension method.
        /// </summary>
        /// <typeparam name="TBindingType"></typeparam>
        /// <param name="property"></param>
        /// <param name="bindable"></param>
        /// <param name="changed"></param>
        /// <param name="onlyWhenChanged"></param>
        /// <returns></returns>
        public static IDisposable BindProperty <TBindingType>(this IBindable bindable, P <TBindingType> property,
                                                              Action <TBindingType> changed, bool onlyWhenChanged = true)
        {
            changed(property.Value);
            if (onlyWhenChanged)
            {
                return
                    (bindable.AddBinding(
                         property.Where(p => !P <TBindingType> .EqualityComparer.Equals(property.Value, property.LastValue)).Subscribe(changed)));
            }

            return(bindable.AddBinding(property.Subscribe(changed)));
        }
Beispiel #5
0
    /// <summary>
    /// Bind to a ViewModel collection.
    /// </summary>
    /// <typeparam name="TCollectionItemType">The type that the collection contains.</typeparam>
    /// <param name="t">This</param>
    /// <param name="collection">The Model Collection to bind to</param>
    /// <param name="added"></param>
    /// <param name="removed"></param>
    /// <returns>The binding class that allows chaining extra options.</returns>
    public static IDisposable BindCollection <TCollectionItemType>(this IBindable t, ObservableCollection <TCollectionItemType> collection, Action <TCollectionItemType> added, Action <TCollectionItemType> removed)
    {
        NotifyCollectionChangedEventHandler collectionChanged = delegate(NotifyCollectionChangedEventArgs args)
        {
            if (added != null && args.NewItems != null)
            {
                foreach (var item in args.NewItems)
                {
                    added((TCollectionItemType)item);
                }
            }

            if (removed != null && args.OldItems != null)
            {
                foreach (var item in args.OldItems)
                {
                    removed((TCollectionItemType)item);
                }
            }
        };

        collection.CollectionChanged += collectionChanged;
        collectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, collection.ToArray()));
        return(t.AddBinding(Disposable.Create(() => collection.CollectionChanged -= collectionChanged)));
    }
Beispiel #6
0
        /// <summary>
        /// Bind to a ViewModel collection.
        /// </summary>
        /// <typeparam name="TCollectionItemType">The type that the collection contains.</typeparam>
        /// <param name="t">This</param>
        /// <param name="collection"></param>
        /// <param name="onAdd"></param>
        /// <param name="onRemove"></param>
        /// <returns>The binding class that allows chaining extra options.</returns>
        public static ModelCollectionBinding <TCollectionItemType> BindCollection <TCollectionItemType>(
            this IBindable t, ModelCollection <TCollectionItemType> collection,
            Action <TCollectionItemType> onAdd,
            Action <TCollectionItemType> onRemove

            )
        {
            var binding = new ModelCollectionBinding <TCollectionItemType>()
            {
                Collection = collection,
                OnAdd      = onAdd,
                OnRemove   = onRemove,
            };

            t.AddBinding(binding);
            binding.Bind();
            return(binding);
        }
Beispiel #7
0
 public static IDisposable BindProperty <TBindingType>(this IBindable bindable, Func <Computed <TBindingType> > sourceProperty, Action <TBindingType> targetSetter)
 {
     return(bindable.AddBinding(sourceProperty().Subscribe(targetSetter)));
 }
Beispiel #8
0
 /// <summary>
 /// Binds to a commands execution and is diposed with the bindable
 /// </summary>
 /// <param name="bindable"></param>
 /// <param name="sourceCommand"></param>
 /// <param name="onExecuted"></param>
 /// <returns></returns>
 public static IDisposable BindCommandExecuted(this IBindable bindable, ICommand sourceCommand, Action onExecuted)
 {
     return(bindable.AddBinding(sourceCommand.Subscribe(delegate { onExecuted(); })));
 }
Beispiel #9
0
 public static IDisposable DisposeWith(this IDisposable disposable, IBindable bindable)
 {
     return(bindable.AddBinding(disposable));
 }