Example #1
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="PropertyObserver{TOwner,T}" /> class.
        /// </summary>
        /// <param name="propertyExpression">The property expression.</param>
        private PropertyObserver([NotNull] Expression <Func <T> > propertyExpression)
        {
            if (propertyExpression == null)
            {
                throw new ArgumentNullException(nameof(propertyExpression));
            }

            this.Observer = PropertyObserver.Observes(propertyExpression, () => this.Update.Raise(), false);
        }
        /// <summary>
        ///     Initializes a new instance of the <see cref="CanExecuteObserver" /> class.
        /// </summary>
        /// <param name="canExecuteExpression">The can execute expression.</param>
        /// <exception cref="ArgumentNullException">canExecuteExpression</exception>
        public CanExecuteObserver([NotNull] Expression <Func <bool> > canExecuteExpression)
        {
            if (canExecuteExpression == null)
            {
                throw new ArgumentNullException(nameof(canExecuteExpression));
            }

            this.Observer = PropertyObserver.Observes(canExecuteExpression, () =>
            {
                this.Update.Raise();
            }, false);
            this.Owner      = this.Observer.Owner ?? throw new NullReferenceException(nameof(this.Owner));
            this.CanExecute = canExecuteExpression.Compile();
        }
Example #3
0
        /// <summary>
        ///     Adds the expression.
        /// </summary>
        /// <param name="propertyExpression">The property expression.</param>
        /// <exception cref="System.ArgumentException">propertyExpression</exception>
        public void AddExpression(Expression <Func <T> > propertyExpression)
        {
            if (this.observedPropertiesExpressions.TryGetValue(propertyExpression.ToString(), out _))
            {
                throw new ArgumentException(
                          $"{propertyExpression} is already being observed.",
                          nameof(propertyExpression));
            }

            var observer = PropertyObserver.Observes(propertyExpression, () => this.Update.Raise());

            this.observedPropertiesExpressions.Add(propertyExpression.ToString(), observer);
            observer.Subscribe();
        }
        /// <summary>
        ///     Initializes a new instance of the <see cref="PropertyObserver{TOwner, T}" /> class.
        /// </summary>
        /// <param name="owner">The owner.</param>
        /// <param name="propertyExpression">The property expression.</param>
        public PropertyObserver(TOwner owner, Expression <Func <TOwner, T> > propertyExpression)
        {
            if (owner == null)
            {
                throw new ArgumentNullException(nameof(owner));
            }

            if (propertyExpression == null)
            {
                throw new ArgumentNullException(nameof(propertyExpression));
            }

            this.Owner    = owner;
            this.Observer = PropertyObserver.Observes(owner, propertyExpression, () => this.Update.Raise(), false);
        }
        /// <summary>
        ///     Initializes a new instance of the <see cref="CanExecuteObserver{TOwner}" /> class.
        /// </summary>
        /// <param name="owner">The owner.</param>
        /// <param name="canExecuteExpression">The can execute expression.</param>
        /// <exception cref="ArgumentNullException"></exception>
        public CanExecuteObserver([NotNull] TOwner owner, [NotNull] Expression <Func <TOwner, bool> > canExecuteExpression)
        {
            if (owner == null)
            {
                throw new ArgumentNullException(nameof(owner));
            }

            if (canExecuteExpression == null)
            {
                throw new ArgumentNullException(nameof(canExecuteExpression));
            }

            this.Owner      = owner;
            this.Observer   = PropertyObserver.Observes(owner, canExecuteExpression, () => this.Update.Raise(), false);
            this.CanExecute = () => canExecuteExpression.Compile()(owner);
        }
Example #6
0
        /// <summary>
        ///     Adds the expression.
        /// </summary>
        /// <typeparam name="TOwner">The type of the owner.</typeparam>
        /// <param name="owner">The owner.</param>
        /// <param name="propertyExpression">The property expression.</param>
        /// <exception cref="ArgumentException">propertyExpression</exception>
        public void AddExpression <TOwner>(TOwner owner, Expression <Func <TOwner, T> > propertyExpression)
            where TOwner : INotifyPropertyChanged

        {
            if (this.observedPropertiesExpressions.TryGetValue(
                    owner.GetHashCode() + propertyExpression.ToString(),
                    out _))
            {
                throw new ArgumentException(
                          $"{propertyExpression} is already being observed.",
                          nameof(propertyExpression));
            }

            var observer = PropertyObserver.Observes(owner, propertyExpression, () => this.Update.Raise());

            this.observedPropertiesExpressions.Add(owner.GetHashCode() + propertyExpression.ToString(), observer);
            observer.Subscribe();
        }