public CommandsViewModel() { ManualRelayCommandNoCondition = new ManualRelayCommand(() => Executed = "ManualRelayCommandNoCondition"); ManualRelayCommand = new ManualRelayCommand(() => Executed = "ManualRelayCommand", () => CanExecute); ManualRelayCommandWithParameter = new ManualRelayCommand <string>(x => Executed = "ManualRelayCommandWithParameter: " + x, _ => CanExecute); RelayCommandNoCondition = new RelayCommand(() => Executed = "RelayCommandNoCondition"); RelayCommand = new RelayCommand(() => Executed = "RelayCommand", () => CanExecute); RelayCommandWithParamater = new RelayCommand <string>(x => Executed = "RelayCommandWithParamater: " + x, x => CanExecute); ObservingRelayCommand = new ObservingRelayCommand(() => Executed = "ObservingRelayCommand", () => CanExecute, this.ObservePropertyChanged(x => x.CanExecute)); ObservingRelayCommandWithParameter = new ObservingRelayCommand <string>(x => Executed = "ObservingRelayCommandWithParameter:" + x, x => CanExecute, this.ObservePropertyChanged(x => x.CanExecute)); var condition = new Condition(this.ObservePropertyChanged(x => x.CanExecute), () => CanExecute); ConditionRelayCommand = new ConditionRelayCommand(() => Executed = "ObservingRelayCommand", condition); ConditionRelayCommandWithParameter = new ConditionRelayCommand <string>(x => Executed = "ConditionRelayCommandWithParameter: " + x, condition); RaiseCanExecuteCommand = new RelayCommand(RaiseCanExecute); RaiseCanExecuteOnOtherThread = new RelayCommand(() => Task.Run(() => RaiseCanExecute())); DelayedToggleCanExecute = new RelayCommand(async() => { await Task.Delay(500).ConfigureAwait(false); CanExecute = !CanExecute; }); }
public void CanExecuteConditionParameter(bool expected) { var observable = new Subject <object>(); var command = new ObservingRelayCommand <int>(_ => { }, _ => expected, observable); Assert.AreEqual(expected, command.CanExecute(0)); }
public void CanExecuteCondition(bool expected) { var observable = new Subject <object>(); var command = new ObservingRelayCommand(() => { }, () => expected, observable); Assert.AreEqual(expected, command.CanExecute()); }
public void NotifiesOnConditionChanged() { var fake = new Fake { IsTrueOrNull = false }; var observable = fake.ObservePropertyChanged(x => x.IsTrueOrNull); var command = new ObservingRelayCommand(() => { }, () => false, observable); int count = 0; command.CanExecuteChanged += (sender, args) => count++; fake.IsTrueOrNull = true; Assert.AreEqual(1, count); }
public void NotifiesOnConditionChanged() { var fake = new FakeInpc { Prop1 = false }; var observable = fake.ToObservable(x => x.Prop1); var command = new ObservingRelayCommand(_ => { }, _ => false, observable, false); int count = 0; command.CanExecuteChanged += (sender, args) => count++; fake.Prop1 = true; Assert.AreEqual(1, count); }
public async Task NotifiesOnConditionChanged() { var fake = new Fake { IsTrueOrNull = false }; int count; using var command = new ObservingRelayCommand(() => { }, () => false, fake.ObservePropertyChanged(x => x.IsTrueOrNull)); count = 0; command.CanExecuteChanged += (sender, args) => count++; fake.IsTrueOrNull = true; await Application.Current.Dispatcher.SimulateYield(); Assert.AreEqual(1, count); }
private void RaiseCanExecute() { ManualRelayCommandNoCondition.RaiseCanExecuteChanged(); ManualRelayCommand.RaiseCanExecuteChanged(); ManualRelayCommandWithParameter.RaiseCanExecuteChanged(); RelayCommandNoCondition.RaiseCanExecuteChanged(); RelayCommand.RaiseCanExecuteChanged(); RelayCommandWithParamater.RaiseCanExecuteChanged(); ObservingRelayCommand.RaiseCanExecuteChanged(); ObservingRelayCommandWithParameter.RaiseCanExecuteChanged(); ConditionRelayCommand.RaiseCanExecuteChanged(); ConditionRelayCommandWithParameter.RaiseCanExecuteChanged(); }
public void ExecuteNotifies() { var invokeCount = 0; var isExecutingCount = 0; var command = new ObservingRelayCommand(() => invokeCount++, () => true, new Subject <object>()); command.ObservePropertyChangedSlim(nameof(command.IsExecuting), false) .Subscribe(_ => isExecutingCount++); Assert.IsFalse(command.IsExecuting); Assert.True(command.CanExecute()); command.Execute(); Assert.IsFalse(command.IsExecuting); Assert.True(command.CanExecute()); Assert.AreEqual(1, invokeCount); Assert.AreEqual(2, isExecutingCount); }