diff options
author | Matheus Neder <matheusneder@gmail.com> | 2017-05-04 21:51:11 +0300 |
---|---|---|
committer | Matheus Neder <matheusneder@gmail.com> | 2017-05-04 21:51:11 +0300 |
commit | 37b3c2b280e60e23ec44c944310a414e5e97837a (patch) | |
tree | 6fea9c8e5ffc870ff7404d93dbe33f06e5077460 | |
parent | 8afcf605ecec7a502500035a71160d1eeaa3d0f1 (diff) |
Revert "Add ObservesProperty overload on DelegateCommand and DelegateCommand Of T that works for complex property. It takes the complex object to subscribe PropertyChanged listener as first parameter and a lambda with what property to observe as second parameter."
This reverts commit 8afcf605ecec7a502500035a71160d1eeaa3d0f1.
-rw-r--r-- | Source/Prism.Tests/Commands/DelegateCommandFixture.cs | 116 | ||||
-rw-r--r-- | Source/Prism/Commands/DelegateCommand.cs | 14 | ||||
-rw-r--r-- | Source/Prism/Commands/DelegateCommandBase.cs | 60 | ||||
-rw-r--r-- | Source/Prism/Commands/DelegateCommand{T}.cs | 14 | ||||
-rw-r--r-- | Source/Prism/Mvvm/PropertySupport.cs | 21 |
5 files changed, 23 insertions, 202 deletions
diff --git a/Source/Prism.Tests/Commands/DelegateCommandFixture.cs b/Source/Prism.Tests/Commands/DelegateCommandFixture.cs index adecfec..c70b2c4 100644 --- a/Source/Prism.Tests/Commands/DelegateCommandFixture.cs +++ b/Source/Prism.Tests/Commands/DelegateCommandFixture.cs @@ -374,7 +374,7 @@ namespace Prism.Tests.Commands command.CanExecuteChanged += delegate { canExecuteChangedRaised = true; }; - RaisePropertyChanged(null); + RaisePropertyChanged(null); Assert.True(canExecuteChangedRaised); } @@ -388,56 +388,12 @@ namespace Prism.Tests.Commands command.CanExecuteChanged += delegate { canExecuteChangedRaised = true; }; - RaisePropertyChanged(null); + RaisePropertyChanged(null); Assert.False(canExecuteChangedRaised); } [Fact] - public void NonGenericDelegateCommandShouldObserveOneComplexProperty() - { - bool canExecuteChangedRaised = false; - - var command = new DelegateCommand(() => { }).ObservesProperty(ComplexProperty, p => p.IntProperty); - - command.CanExecuteChanged += delegate { canExecuteChangedRaised = true; }; - - ComplexProperty.IntProperty = 10; - - Assert.True(canExecuteChangedRaised); - } - - [Fact] - public void NonGenericDelegateCommandShouldObserveMultipleComplexProperties() - { - bool canExecuteChangedRaised = false; - - var command = new DelegateCommand(() => { }).ObservesProperty(ComplexProperty, p => p.IntProperty) - .ObservesProperty(ComplexProperty, p => p.BoolProperty); - - command.CanExecuteChanged += delegate { canExecuteChangedRaised = true; }; - - ComplexProperty.IntProperty = 10; - - Assert.True(canExecuteChangedRaised); - - canExecuteChangedRaised = false; - - ComplexProperty.BoolProperty = true; - - Assert.True(canExecuteChangedRaised); - } - - [Fact] - public void NonGenericDelegateCommandShouldNotObserveDuplicateComplexProperties() - { - Assert.Throws<ArgumentException>(() => - { - DelegateCommand command = new DelegateCommand(() => { }).ObservesProperty(ComplexProperty, p => p.IntProperty).ObservesProperty(ComplexProperty, p => p.IntProperty); - }); - } - - [Fact] public void GenericDelegateCommandShouldObserveCanExecute() { bool canExecuteChangedRaised = false; @@ -576,54 +532,12 @@ namespace Prism.Tests.Commands command.CanExecuteChanged += delegate { canExecuteChangedRaised = true; }; - RaisePropertyChanged(null); + RaisePropertyChanged(null); Assert.False(canExecuteChangedRaised); } - [Fact] - public void GenericDelegateCommandShouldObserveOneComplexProperty() - { - bool canExecuteChangedRaised = false; - - var command = new DelegateCommand<object>((o) => { }).ObservesProperty(ComplexProperty, p => p.IntProperty); - - command.CanExecuteChanged += delegate { canExecuteChangedRaised = true; }; - - ComplexProperty.IntProperty = 10; - - Assert.True(canExecuteChangedRaised); - } - - [Fact] - public void GenericDelegateCommandShouldObserveMultipleComplexProperties() - { - bool canExecuteChangedRaised = false; - - var command = new DelegateCommand<object>((o) => { }).ObservesProperty(ComplexProperty, p => p.IntProperty) - .ObservesProperty(ComplexProperty, p => p.BoolProperty); - - command.CanExecuteChanged += delegate { canExecuteChangedRaised = true; }; - - ComplexProperty.IntProperty = 10; - - Assert.True(canExecuteChangedRaised); - - canExecuteChangedRaised = false; - - ComplexProperty.BoolProperty = true; - Assert.True(canExecuteChangedRaised); - } - - [Fact] - public void GenericDelegateCommandShouldNotObserveDuplicateComplexProperties() - { - Assert.Throws<ArgumentException>(() => - { - DelegateCommand<object> command = new DelegateCommand<object>((o) => { }).ObservesProperty(ComplexProperty, p => p.IntProperty).ObservesProperty(ComplexProperty, p => p.IntProperty); - }); - } private bool _boolProperty; public bool BoolProperty @@ -639,30 +553,6 @@ namespace Prism.Tests.Commands set { SetProperty(ref _intProperty, value); } } - public class ComplexType : BindableBase - { - private bool _boolProperty; - public bool BoolProperty - { - get { return _boolProperty; } - set { SetProperty(ref _boolProperty, value); } - } - - private int _intProperty; - public int IntProperty - { - get { return _intProperty; } - set { SetProperty(ref _intProperty, value); } - } - } - - private ComplexType _complexProperty = new ComplexType(); - public ComplexType ComplexProperty - { - get { return _complexProperty; } - set { SetProperty(ref _complexProperty, value); } - } - class CanExecutChangeHandler { public bool CanExeucteChangedHandlerCalled; diff --git a/Source/Prism/Commands/DelegateCommand.cs b/Source/Prism/Commands/DelegateCommand.cs index d8bb1b0..6111615 100644 --- a/Source/Prism/Commands/DelegateCommand.cs +++ b/Source/Prism/Commands/DelegateCommand.cs @@ -80,20 +80,6 @@ namespace Prism.Commands } /// <summary> - /// Observes a property that implements INotifyPropertyChanged, and automatically calls DelegateCommandBase.RaiseCanExecuteChanged on property changed notifications. - /// </summary> - /// <typeparam name="T">The object type containing the property specified in the expression.</typeparam> - /// <typeparam name="TProp">The property type specified in the expression.</typeparam> - /// <param name="target">The object containing the property to observe</param> - /// <param name="propertyExpression">The property expression. Example: ObservesProperty(Person, p => p.FirstName).</param> - /// <returns>The current instance of DelegateCommand</returns> - public DelegateCommand ObservesProperty<T, TProp>(T target, Expression<Func<T, TProp>> propertyExpression) - { - ObservesPropertyInternal(target, propertyExpression); - return this; - } - - /// <summary> /// Observes a property that is used to determine if this command can execute, and if it implements INotifyPropertyChanged it will automatically call DelegateCommandBase.RaiseCanExecuteChanged on property changed notifications. /// </summary> /// <param name="canExecuteExpression">The property expression. Example: ObservesCanExecute(() => PropertyName).</param> diff --git a/Source/Prism/Commands/DelegateCommandBase.cs b/Source/Prism/Commands/DelegateCommandBase.cs index d9c1a38..aea000b 100644 --- a/Source/Prism/Commands/DelegateCommandBase.cs +++ b/Source/Prism/Commands/DelegateCommandBase.cs @@ -18,8 +18,8 @@ namespace Prism.Commands private SynchronizationContext _synchronizationContext; - private readonly HashSet<Tuple<object, string>> _propertiesToObserve = new HashSet<Tuple<object, string>>(); - private readonly HashSet<INotifyPropertyChanged> _inpc = new HashSet<INotifyPropertyChanged>(); + readonly HashSet<string> _propertiesToObserve = new HashSet<string>(); + private INotifyPropertyChanged _inpc; /// <summary> @@ -85,58 +85,38 @@ namespace Prism.Commands /// <param name="propertyExpression">The property expression. Example: ObservesProperty(() => PropertyName).</param> protected internal void ObservesPropertyInternal<T>(Expression<Func<T>> propertyExpression) { - var constantExpression = (propertyExpression?.Body as MemberExpression)?.Expression as ConstantExpression; - var objectToObserve = constantExpression?.Value; - ObservesPropertyInternal(objectToObserve, PropertySupport.ExtractPropertyName(propertyExpression)); + AddPropertyToObserve(PropertySupport.ExtractPropertyName(propertyExpression)); + HookInpc(propertyExpression.Body as MemberExpression); } - protected internal void ObservesPropertyInternal<T, TProp>(T target, Expression<Func<T, TProp>> propertyExpression) + protected void HookInpc(MemberExpression expression) { - ObservesPropertyInternal(target, PropertySupport.ExtractPropertyName(propertyExpression)); - } - - private void ObservesPropertyInternal(object target, string propertyName) - { - AddPropertyToObserve(target, propertyName); - HookInpc(target); - } - - protected void HookInpc(object target) - { - if (target == null) - throw new ArgumentNullException(nameof(target)); - - var notifyPropertyChangedObject = target as INotifyPropertyChanged; + if (expression == null) + return; - if (notifyPropertyChangedObject == null) - throw new InvalidOperationException(string.Format("{0} object must implement INotifyPropertyChanged.", nameof(target))); - - if (!_inpc.Contains(notifyPropertyChangedObject)) + if (_inpc == null) { - notifyPropertyChangedObject.PropertyChanged += Inpc_PropertyChanged; - _inpc.Add(notifyPropertyChangedObject); + var constantExpression = expression.Expression as ConstantExpression; + if (constantExpression != null) + { + _inpc = constantExpression.Value as INotifyPropertyChanged; + if (_inpc != null) + _inpc.PropertyChanged += Inpc_PropertyChanged; + } } } - protected void AddPropertyToObserve(object target, string property) + protected void AddPropertyToObserve(string property) { - if (target == null) - throw new ArgumentNullException(nameof(target)); - - if (string.IsNullOrEmpty(property)) - throw new ArgumentNullException(nameof(property)); - - var propertyToObserve = new Tuple<object, string>(target, property); - - if (_propertiesToObserve.Contains(propertyToObserve)) - throw new ArgumentException(String.Format("{0} of {1} is already being observed.", property, target.GetType().Name)); + if (_propertiesToObserve.Contains(property)) + throw new ArgumentException(String.Format("{0} is already being observed.", property)); - _propertiesToObserve.Add(propertyToObserve); + _propertiesToObserve.Add(property); } void Inpc_PropertyChanged(object sender, PropertyChangedEventArgs e) { - if (_propertiesToObserve.Contains(new Tuple<object, string>(sender, e.PropertyName)) || (string.IsNullOrEmpty(e.PropertyName) && _propertiesToObserve.Count > 0)) + if (_propertiesToObserve.Contains(e.PropertyName) || (string.IsNullOrEmpty(e.PropertyName) && _propertiesToObserve.Count > 0)) { RaiseCanExecuteChanged(); } diff --git a/Source/Prism/Commands/DelegateCommand{T}.cs b/Source/Prism/Commands/DelegateCommand{T}.cs index cebfb66..085e3b1 100644 --- a/Source/Prism/Commands/DelegateCommand{T}.cs +++ b/Source/Prism/Commands/DelegateCommand{T}.cs @@ -116,20 +116,6 @@ namespace Prism.Commands } /// <summary> - /// Observes a property that implements INotifyPropertyChanged, and automatically calls DelegateCommandBase.RaiseCanExecuteChanged on property changed notifications. - /// </summary> - /// <typeparam name="TType">The object type containing the property specified in the expression.</typeparam> - /// <typeparam name="TProp">The property type specified in the expression.</typeparam> - /// <param name="target">The object containing the property to observe</param> - /// <param name="propertyExpression">The property expression. Example: ObservesProperty(Person, p => p.FirstName).</param> - /// <returns>The current instance of DelegateCommand</returns> - public DelegateCommand<T> ObservesProperty<TType, TProp>(TType target, Expression<Func<TType, TProp>> propertyExpression) - { - ObservesPropertyInternal(target, propertyExpression); - return this; - } - - /// <summary> /// Observes a property that is used to determine if this command can execute, and if it implements INotifyPropertyChanged it will automatically call DelegateCommandBase.RaiseCanExecuteChanged on property changed notifications. /// </summary> /// <param name="canExecuteExpression">The property expression. Example: ObservesCanExecute(() => PropertyName).</param> diff --git a/Source/Prism/Mvvm/PropertySupport.cs b/Source/Prism/Mvvm/PropertySupport.cs index 133e5ca..6f26a68 100644 --- a/Source/Prism/Mvvm/PropertySupport.cs +++ b/Source/Prism/Mvvm/PropertySupport.cs @@ -33,27 +33,6 @@ namespace Prism.Mvvm } /// <summary> - /// Extracts the property name from a property expression. - /// </summary> - /// <typeparam name="T">The object type containing the property specified in the expression.</typeparam> - /// <typeparam name="TProp">The property type specified in the expression.</typeparam> - /// <param name="propertyExpression">The property expression (e.g. p => p.PropertyName)</param> - /// <returns>The name of the property.</returns> - /// <exception cref="ArgumentNullException">Thrown if the <paramref name="propertyExpression"/> is null.</exception> - /// <exception cref="ArgumentException">Thrown when the expression is:<br/> - /// Not a <see cref="MemberExpression"/><br/> - /// The <see cref="MemberExpression"/> does not represent a property.<br/> - /// Or, the property is static. - /// </exception> - public static string ExtractPropertyName<T, TProp>(Expression<Func<T, TProp>> propertyExpression) - { - if (propertyExpression == null) - throw new ArgumentNullException(nameof(propertyExpression)); - - return ExtractPropertyNameFromLambda(propertyExpression); - } - - /// <summary> /// Extracts the property name from a LambdaExpression. /// </summary> /// <param name="expression">The LambdaExpression</param> |