Example #1
0
 /// <summary>
 /// Creates a new asynchronous command, with the specified asynchronous delegate as its implementation which can use a weak event handler or the command manager.
 /// </summary>
 public AsyncCommand(Func <object, Task> executeAsync, Func <object, bool> canExecute, bool useCommandManager)
 {
     _canExecuteChanged = new WeakCanExecuteChanged(this);
     _executeAsync      = executeAsync;
     _canExecute        = canExecute;
     _useCommandManager = useCommandManager;
 }
        public void CanExecuteChanged_UnsubscribeNonexistentEvent_DoesNothing()
        {
            var          sender                     = new object();
            object       weakObservedSender         = null;
            object       strongObservedSender       = null;
            object       unsubscribedObservedSender = null;
            var          command                    = new WeakCanExecuteChanged(sender);
            EventHandler weakSubscription           = (s, _) =>
            {
                weakObservedSender = s;
            };
            EventHandler strongSubscription = (s, _) =>
            {
                strongObservedSender = s;
            };
            EventHandler unsubscribedSubscription = (s, _) =>
            {
                unsubscribedObservedSender = s;
            };

            command.CanExecuteChanged += strongSubscription;
            command.CanExecuteChanged += weakSubscription;
            weakSubscription           = null;
            GC.Collect();
            command.CanExecuteChanged -= unsubscribedSubscription;
            command.OnCanExecuteChanged();

            Assert.Null(weakObservedSender);
            Assert.Same(sender, strongObservedSender);
            Assert.Null(unsubscribedObservedSender);

            GC.KeepAlive(strongSubscription);
            GC.KeepAlive(unsubscribedSubscription);
        }
        public void CanExecuteChanged_IsWeakEvent()
        {
            var          sender               = new object();
            object       weakObservedSender   = null;
            object       strongObservedSender = null;
            var          command              = new WeakCanExecuteChanged(sender);
            EventHandler weakSubscription     = (s, _) =>
            {
                weakObservedSender = s;
            };
            EventHandler strongSubscription = (s, _) =>
            {
                strongObservedSender = s;
            };

            command.CanExecuteChanged += weakSubscription;
            command.CanExecuteChanged += strongSubscription;
            weakSubscription           = null;
            GC.Collect();
            command.OnCanExecuteChanged();

            Assert.Null(weakObservedSender);
            Assert.Same(sender, strongObservedSender);

            GC.KeepAlive(strongSubscription);
        }
Example #4
0
        /// <summary>
        /// Creates a new command.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        /// <param name="canExecute">The execution status logic.</param>
        public DelegateCommand(Action <object> execute, Predicate <object> canExecute)
        {
            if (execute == null)
            {
                throw new ArgumentNullException(nameof(execute));
            }

            _execute           = execute;
            _canExecute        = canExecute;
            _canExecuteChanged = new WeakCanExecuteChanged(this);
        }
        public void CanExecuteChanged_Unsubscribed_IsNotNotified()
        {
            var          sender         = new object();
            object       observedSender = null;
            var          command        = new WeakCanExecuteChanged(sender);
            EventHandler subscription   = (s, _) =>
            {
                observedSender = s;
            };

            command.CanExecuteChanged += subscription;
            command.CanExecuteChanged -= subscription;
            command.OnCanExecuteChanged();

            Assert.Null(observedSender);

            GC.KeepAlive(subscription);
        }
 /// <summary>
 /// Base constructor for asynchronous command.
 /// </summary>
 /// <param name="canExecute">The implementation of <see cref="ICommand.CanExecute(object)"/>.</param>
 protected AsyncCommandBase(Func <object, bool> canExecute = null)
 {
     _cancelCommand     = new CancelAsyncCommand();
     _canExecute        = canExecute;
     _canExecuteChanged = new WeakCanExecuteChanged(this);
 }
Example #7
0
 /// <summary>
 /// Creates a new cancel command with a new cancellation token. This command is initially enabled, with an uncanceled cancellation token.
 /// </summary>
 public CancelCommand()
 {
     _canExecuteChanged = new WeakCanExecuteChanged(this);
 }
 /// <summary>
 /// Creates a new cancel command with a new cancellation token.
 /// This command is initially enabled, with an uncanceled cancellation token.
 /// </summary>
 public CancelCommand()
 {
     _canExecuteChanged = new WeakCanExecuteChanged(this);
 }