wpf CommandManager.InvalidateRequerySuggested() 不够快。我能做什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1751966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:13:39  来源:igfitidea点击:

CommandManager.InvalidateRequerySuggested() isn't fast enough. What can I do?

wpficommand

提问by Rob

Short Version

精简版

Calls to CommandManager.InvalidateRequerySuggested()take far longer to take effect than I would like (1-2 second delay before UI controls become disabled).

调用CommandManager.InvalidateRequerySuggested()需要的时间比我想要的要长得多(在 UI 控件被禁用之前延迟 1-2 秒)。

Long Version

长版

I have a system where I submit tasks to a background-thread based task processor. This submit happens on the WPF UI thread.

我有一个系统,可以将任务提交给基于后台线程的任务处理器。此提交发生在 WPF UI 线程上。

When this submit happens, the object that manages my background thread does two things:

当这个提交发生时,管理我的后台线程的对象会做两件事:

  1. It raises a "busy" event (still on the UI thread) that several view models respond to; when they receive this event, they set an IsEnabledflag on themselves to false. Controls in my views, which are databound to this property, are immediately grayed out, which is what I would expect.

  2. It informs my WPF ICommandobjects that they should not be allowed to execute (again, still on the UI thread). Because there is nothing like INotifyPropertyChangedfor ICommandobjects, I am forced to call CommandManager.InvalidateRequerySuggested()to force WPF to reconsider all of my command objects' CanExecutestates (yes, I actually do need to do this: otherwise, none of these controls become disabled). Unlike item 1, though, it takes a significantly longer time for my buttons/menu items/etc that are using ICommandobjects to visually change to a disabled state than it does for the UI controls that have their IsEnabledproperty manually set.

  1. 它引发了几个视图模型响应的“忙”事件(仍在 UI 线程上);当他们收到此事件时,他们将IsEnabled自己设置为false。我的视图中的控件(数据绑定到此属性)立即变灰,这正是我所期望的。

  2. 它通知我的 WPFICommand对象不应该被允许执行(同样,仍然在 UI 线程上)。因为没有什么比INotifyPropertyChangedICommand的对象,我不得不打电话CommandManager.InvalidateRequerySuggested()迫使WPF重新考虑我所有的命令对象的CanExecute状态(是的,其实我需要做的:否则,没有这些控件被禁用)。但是,与第 1 项不同,我的按钮/菜单项/等使用ICommand对象从视觉上更改为禁用状态所需的时间比IsEnabled手动设置其属性的 UI 控件所需的时间要长得多。

The problem is, from a UX point of view, this looks awful; half of my controls are immediately grayed out (because their IsEnabledproperty is set to false), and then a full 1-2 seconds later, the other half of my controls follow suit (because their CanExecutemethods are finally re-evaluated).

问题是,从用户体验的角度来看,这看起来很糟糕;我的一半控件立即变灰(因为它们的IsEnabled属性设置为 false),然后整整 1-2 秒后,我的另一半控件也效仿(因为它们的CanExecute方法最终被重新评估)。

So, part 1 of my question:
As silly as it sounds to ask, is there a way I can make CommandManager.InvalidateRequerySuggested()do it's job faster? I suspect that there isn't.

所以,我的问题的第 1 部分:
尽管问起来很愚蠢,但有没有办法让我CommandManager.InvalidateRequerySuggested()更快地完成它的工作?我怀疑没有。

Fair enough, part 2 of my question:
How can I work around this? I'd prefer all of my controls be disabled at the same time. It just looks unprofessional and awkward otherwise. Any ideas? :-)

很公平,我的问题的第 2 部分:
我该如何解决这个问题?我希望同时禁用所有控件。否则看起来不专业和尴尬。有任何想法吗?:-)

回答by Tomá? Kafka

CommandManager.InvalidateRequerySuggested()tries to validate all commands, which is totally ineffective (and in your case slow) - on every change, you are asking every commandto recheck its CanExecute()!

CommandManager.InvalidateRequerySuggested()尝试验证所有命令,这是完全无效的(在您的情况下很慢) - 在每次更改时,您都要求每个命令重新检查它的CanExecute()!

You'd need the command to know on which objects and properties is its CanExecutedependent, and suggest requery only when they change. That way, if you change a property of an object, only commands that depend on it will change their state.

您需要该命令来了解其CanExecute依赖于哪些对象和属性,并建议仅在它们更改时重新查询。这样,如果您更改对象的属性,则只有依赖它的命令才会更改其状态。

This is how I solved the problem, but at first, a teaser:

这就是我解决问题的方法,但首先是一个预告片:

// in ViewModel's constructor - add a code to public ICommand:
this.DoStuffWithParameterCommand = new DelegateCommand<object>(
    parameter =>
        {
            //do work with parameter (remember to check against null)
        },
    parameter => 
        {
            //can this command execute? return true or false
        }
    )
    .ListenOn(whichObject, n => n.ObjectProperty /*type safe!*/, this.Dispatcher /*we need to pass UI dispatcher here*/)
    .ListenOn(anotherObject, n => n.AnotherObjectProperty, this.Dispatcher); // chain calling!

The command is listening on NotifyPropertyChangedevents from object that affect whether it can execute, and invokes the check only when a requery is needed.

该命令正在侦听NotifyPropertyChanged来自对象的影响它是否可以执行的事件,并且仅在需要重新查询时调用检查。

Now, a lot of code (part of our in-house framework) to do this:

现在,很多代码(我们内部框架的一部分)可以做到这一点:

I use DelegateCommandfrom Prism, that looks like this:

DelegateCommand从 Prism使用,看起来像这样:

/// <summary>
///     This class allows delegating the commanding logic to methods passed as parameters,
///     and enables a View to bind commands to objects that are not part of the element tree.
/// </summary>
public class DelegateCommand : ICommand
{
    #region Constructors

    /// <summary>
    ///     Constructor
    /// </summary>
    public DelegateCommand(Action executeMethod)
        : this(executeMethod, null, false)
    {
    }

    /// <summary>
    ///     Constructor
    /// </summary>
    public DelegateCommand(Action executeMethod, Func<bool> canExecuteMethod)
        : this(executeMethod, canExecuteMethod, false)
    {
    }

    /// <summary>
    ///     Constructor
    /// </summary>
    public DelegateCommand(Action executeMethod, Func<bool> canExecuteMethod, bool isAutomaticRequeryDisabled)
    {
        if (executeMethod == null)
        {
            throw new ArgumentNullException("executeMethod");
        }

        _executeMethod = executeMethod;
        _canExecuteMethod = canExecuteMethod;
        _isAutomaticRequeryDisabled = isAutomaticRequeryDisabled;

        this.RaiseCanExecuteChanged();
    }

    #endregion

    #region Public Methods

    /// <summary>
    ///     Method to determine if the command can be executed
    /// </summary>
    public bool CanExecute()
    {
        if (_canExecuteMethod != null)
        {
            return _canExecuteMethod();
        }
        return true;
    }

    /// <summary>
    ///     Execution of the command
    /// </summary>
    public void Execute()
    {
        if (_executeMethod != null)
        {
            _executeMethod();
        }
    }

    /// <summary>
    ///     Property to enable or disable CommandManager's automatic requery on this command
    /// </summary>
    public bool IsAutomaticRequeryDisabled
    {
        get
        {
            return _isAutomaticRequeryDisabled;
        }
        set
        {
            if (_isAutomaticRequeryDisabled != value)
            {
                if (value)
                {
                    CommandManagerHelper.RemoveHandlersFromRequerySuggested(_canExecuteChangedHandlers);
                }
                else
                {
                    CommandManagerHelper.AddHandlersToRequerySuggested(_canExecuteChangedHandlers);
                }
                _isAutomaticRequeryDisabled = value;
            }
        }
    }

    /// <summary>
    ///     Raises the CanExecuteChaged event
    /// </summary>
    public void RaiseCanExecuteChanged()
    {
        OnCanExecuteChanged();
    }

    /// <summary>
    ///     Protected virtual method to raise CanExecuteChanged event
    /// </summary>
    protected virtual void OnCanExecuteChanged()
    {
        CommandManagerHelper.CallWeakReferenceHandlers(_canExecuteChangedHandlers);
    }

    #endregion

    #region ICommand Members

    /// <summary>
    ///     ICommand.CanExecuteChanged implementation
    /// </summary>
    public event EventHandler CanExecuteChanged
    {
        add
        {
            if (!_isAutomaticRequeryDisabled)
            {
                CommandManager.RequerySuggested += value;
            }
            CommandManagerHelper.AddWeakReferenceHandler(ref _canExecuteChangedHandlers, value, 2);
        }
        remove
        {
            if (!_isAutomaticRequeryDisabled)
            {
                CommandManager.RequerySuggested -= value;
            }
            CommandManagerHelper.RemoveWeakReferenceHandler(_canExecuteChangedHandlers, value);
        }
    }

    bool ICommand.CanExecute(object parameter)
    {
        return CanExecute();
    }

    void ICommand.Execute(object parameter)
    {
        Execute();
    }

    #endregion

    #region Data

    private readonly Action _executeMethod = null;
    private readonly Func<bool> _canExecuteMethod = null;
    private bool _isAutomaticRequeryDisabled = false;
    private List<WeakReference> _canExecuteChangedHandlers;

    #endregion
}

/// <summary>
///     This class allows delegating the commanding logic to methods passed as parameters,
///     and enables a View to bind commands to objects that are not part of the element tree.
/// </summary>
/// <typeparam name="T">Type of the parameter passed to the delegates</typeparam>
public class DelegateCommand<T> : ICommand
{
    #region Constructors

    /// <summary>
    ///     Constructor
    /// </summary>
    public DelegateCommand(Action<T> executeMethod)
        : this(executeMethod, null, false)
    {
    }

    /// <summary>
    ///     Constructor
    /// </summary>
    public DelegateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod)
        : this(executeMethod, canExecuteMethod, false)
    {
    }

    /// <summary>
    ///     Constructor
    /// </summary>
    public DelegateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod, bool isAutomaticRequeryDisabled)
    {
        if (executeMethod == null)
        {
            throw new ArgumentNullException("executeMethod");
        }

        _executeMethod = executeMethod;
        _canExecuteMethod = canExecuteMethod;
        _isAutomaticRequeryDisabled = isAutomaticRequeryDisabled;
    }

    #endregion

    #region Public Methods

    /// <summary>
    ///     Method to determine if the command can be executed
    /// </summary>
    public bool CanExecute(T parameter)
    {
        if (_canExecuteMethod != null)
        {
            return _canExecuteMethod(parameter);
        }
        return true;
    }

    /// <summary>
    ///     Execution of the command
    /// </summary>
    public void Execute(T parameter)
    {
        if (_executeMethod != null)
        {
            _executeMethod(parameter);
        }
    }

    /// <summary>
    ///     Raises the CanExecuteChaged event
    /// </summary>
    public void RaiseCanExecuteChanged()
    {
        OnCanExecuteChanged();
    }

    /// <summary>
    ///     Protected virtual method to raise CanExecuteChanged event
    /// </summary>
    protected virtual void OnCanExecuteChanged()
    {
        CommandManagerHelper.CallWeakReferenceHandlers(_canExecuteChangedHandlers);
    }

    /// <summary>
    ///     Property to enable or disable CommandManager's automatic requery on this command
    /// </summary>
    public bool IsAutomaticRequeryDisabled
    {
        get
        {
            return _isAutomaticRequeryDisabled;
        }
        set
        {
            if (_isAutomaticRequeryDisabled != value)
            {
                if (value)
                {
                    CommandManagerHelper.RemoveHandlersFromRequerySuggested(_canExecuteChangedHandlers);
                }
                else
                {
                    CommandManagerHelper.AddHandlersToRequerySuggested(_canExecuteChangedHandlers);
                }
                _isAutomaticRequeryDisabled = value;
            }
        }
    }

    #endregion

    #region ICommand Members

    /// <summary>
    ///     ICommand.CanExecuteChanged implementation
    /// </summary>
    public event EventHandler CanExecuteChanged
    {
        add
        {
            if (!_isAutomaticRequeryDisabled)
            {
                CommandManager.RequerySuggested += value;
            }
            CommandManagerHelper.AddWeakReferenceHandler(ref _canExecuteChangedHandlers, value, 2);
        }
        remove
        {
            if (!_isAutomaticRequeryDisabled)
            {
                CommandManager.RequerySuggested -= value;
            }
            CommandManagerHelper.RemoveWeakReferenceHandler(_canExecuteChangedHandlers, value);
        }
    }

    bool ICommand.CanExecute(object parameter)
    {
        // if T is of value type and the parameter is not
        // set yet, then return false if CanExecute delegate
        // exists, else return true
        if (parameter == null &&
            typeof(T).IsValueType)
        {
            return (_canExecuteMethod == null);
        }
        return CanExecute((T)parameter);
    }

    void ICommand.Execute(object parameter)
    {
        Execute((T)parameter);
    }

    #endregion

    #region Data

    private readonly Action<T> _executeMethod = null;
    private readonly Func<T, bool> _canExecuteMethod = null;
    private bool _isAutomaticRequeryDisabled = false;
    private List<WeakReference> _canExecuteChangedHandlers;

    #endregion
}

/// <summary>
///     This class contains methods for the CommandManager that help avoid memory leaks by
///     using weak references.
/// </summary>
internal class CommandManagerHelper
{
    internal static void CallWeakReferenceHandlers(List<WeakReference> handlers)
    {
        if (handlers != null)
        {
            // Take a snapshot of the handlers before we call out to them since the handlers
            // could cause the array to me modified while we are reading it.

            EventHandler[] callees = new EventHandler[handlers.Count];
            int count = 0;

            for (int i = handlers.Count - 1; i >= 0; i--)
            {
                WeakReference reference = handlers[i];
                EventHandler handler = reference.Target as EventHandler;
                if (handler == null)
                {
                    // Clean up old handlers that have been collected
                    handlers.RemoveAt(i);
                }
                else
                {
                    callees[count] = handler;
                    count++;
                }
            }

            // Call the handlers that we snapshotted
            for (int i = 0; i < count; i++)
            {
                EventHandler handler = callees[i];
                handler(null, EventArgs.Empty);
            }
        }
    }

    internal static void AddHandlersToRequerySuggested(List<WeakReference> handlers)
    {
        if (handlers != null)
        {
            foreach (WeakReference handlerRef in handlers)
            {
                EventHandler handler = handlerRef.Target as EventHandler;
                if (handler != null)
                {
                    CommandManager.RequerySuggested += handler;
                }
            }
        }
    }

    internal static void RemoveHandlersFromRequerySuggested(List<WeakReference> handlers)
    {
        if (handlers != null)
        {
            foreach (WeakReference handlerRef in handlers)
            {
                EventHandler handler = handlerRef.Target as EventHandler;
                if (handler != null)
                {
                    CommandManager.RequerySuggested -= handler;
                }
            }
        }
    }

    internal static void AddWeakReferenceHandler(ref List<WeakReference> handlers, EventHandler handler)
    {
        AddWeakReferenceHandler(ref handlers, handler, -1);
    }

    internal static void AddWeakReferenceHandler(ref List<WeakReference> handlers, EventHandler handler, int defaultListSize)
    {
        if (handlers == null)
        {
            handlers = (defaultListSize > 0 ? new List<WeakReference>(defaultListSize) : new List<WeakReference>());
        }

        handlers.Add(new WeakReference(handler));
    }

    internal static void RemoveWeakReferenceHandler(List<WeakReference> handlers, EventHandler handler)
    {
        if (handlers != null)
        {
            for (int i = handlers.Count - 1; i >= 0; i--)
            {
                WeakReference reference = handlers[i];
                EventHandler existingHandler = reference.Target as EventHandler;
                if ((existingHandler == null) || (existingHandler == handler))
                {
                    // Clean up old handlers that have been collected
                    // in addition to the handler that is to be removed.
                    handlers.RemoveAt(i);
                }
            }
        }
    }
}

I have then written a ListenOnextension method, that 'binds' the command to a property, and invokes its RaiseCanExecuteChanged:

然后我编写了一个ListenOn扩展方法,将命令“绑定”到一个属性,并调用它的RaiseCanExecuteChanged

public static class DelegateCommandExtensions
{
    /// <summary>
    /// Makes DelegateCommnand listen on PropertyChanged events of some object,
    /// so that DelegateCommnand can update its IsEnabled property.
    /// </summary>
    public static DelegateCommand ListenOn<ObservedType, PropertyType>
        (this DelegateCommand delegateCommand, 
        ObservedType observedObject, 
        Expression<Func<ObservedType, PropertyType>> propertyExpression,
        Dispatcher dispatcher)
        where ObservedType : INotifyPropertyChanged
    {
        //string propertyName = observedObject.GetPropertyName(propertyExpression);
        string propertyName = NotifyPropertyChangedBaseExtensions.GetPropertyName(propertyExpression);

        observedObject.PropertyChanged += (sender, e) =>
        {
            if (e.PropertyName == propertyName)
            {
                if (dispatcher != null)
                {
                    ThreadTools.RunInDispatcher(dispatcher, delegateCommand.RaiseCanExecuteChanged);
                }
                else
                {
                    delegateCommand.RaiseCanExecuteChanged();
                }
            }
        };

        return delegateCommand; //chain calling
    }

    /// <summary>
    /// Makes DelegateCommnand listen on PropertyChanged events of some object,
    /// so that DelegateCommnand can update its IsEnabled property.
    /// </summary>
    public static DelegateCommand<T> ListenOn<T, ObservedType, PropertyType>
        (this DelegateCommand<T> delegateCommand, 
        ObservedType observedObject, 
        Expression<Func<ObservedType, PropertyType>> propertyExpression,
        Dispatcher dispatcher)
        where ObservedType : INotifyPropertyChanged
    {
        //string propertyName = observedObject.GetPropertyName(propertyExpression);
        string propertyName = NotifyPropertyChangedBaseExtensions.GetPropertyName(propertyExpression);

        observedObject.PropertyChanged += (object sender, PropertyChangedEventArgs e) =>
        {
            if (e.PropertyName == propertyName)
            {
                if (dispatcher != null)
                {
                    ThreadTools.RunInDispatcher(dispatcher, delegateCommand.RaiseCanExecuteChanged);
                }
                else
                {
                    delegateCommand.RaiseCanExecuteChanged();
                }
            }
        };

        return delegateCommand; //chain calling
    }
}

You then need the following extension to NotifyPropertyChanged

然后您需要以下扩展名 NotifyPropertyChanged

    /// <summary>
/// <see cref="http://dotnet.dzone.com/news/silverlightwpf-implementing"/>
/// </summary>
public static class NotifyPropertyChangedBaseExtensions
{
    /// <summary>
    /// Raises PropertyChanged event.
    /// To use: call the extension method with this: this.OnPropertyChanged(n => n.Title);
    /// </summary>
    /// <typeparam name="T">Property owner</typeparam>
    /// <typeparam name="TProperty">Type of property</typeparam>
    /// <param name="observableBase"></param>
    /// <param name="expression">Property expression like 'n => n.Property'</param>
    public static void OnPropertyChanged<T, TProperty>(this T observableBase, Expression<Func<T, TProperty>> expression) where T : INotifyPropertyChangedWithRaise
    {
        observableBase.OnPropertyChanged(GetPropertyName<T, TProperty>(expression));
    }

    public static string GetPropertyName<T, TProperty>(Expression<Func<T, TProperty>> expression) where T : INotifyPropertyChanged
    {
        if (expression == null)
            throw new ArgumentNullException("expression");

        var lambda = expression as LambdaExpression;
        MemberExpression memberExpression;
        if (lambda.Body is UnaryExpression)
        {
            var unaryExpression = lambda.Body as UnaryExpression;
            memberExpression = unaryExpression.Operand as MemberExpression;
        }
        else
        {
            memberExpression = lambda.Body as MemberExpression;
        }

        if (memberExpression == null)
            throw new ArgumentException("Please provide a lambda expression like 'n => n.PropertyName'");

        MemberInfo memberInfo = memberExpression.Member;

        if (String.IsNullOrEmpty(memberInfo.Name))
            throw new ArgumentException("'expression' did not provide a property name.");

        return memberInfo.Name;
    }
}

where INotifyPropertyChangedWithRaiseis this (it estabilishes standard interface for raising NotifyPropertyChanged events):

INotifyPropertyChangedWithRaise是哪里(它建立了用于引发 NotifyPropertyChanged 事件的标准接口):

public interface INotifyPropertyChangedWithRaise : INotifyPropertyChanged
{
    void OnPropertyChanged(string propertyName);
}

Last piece of puzzle is this:

最后一块拼图是这样的:

public class ThreadTools
{
    public static void RunInDispatcher(Dispatcher dispatcher, Action action)
    {
        RunInDispatcher(dispatcher, DispatcherPriority.Normal, action);
    }

        public static void RunInDispatcher(Dispatcher dispatcher, DispatcherPriority priority, Action action)
    {
        if (action == null) { return; }

        if (dispatcher.CheckAccess())
        {
            // we are already on thread associated with the dispatcher -> just call action
            try
            {
                action();
            }
            catch (Exception ex)
            {
                //Log error here!
            }
        }
        else
        {
            // we are on different thread, invoke action on dispatcher's thread
            dispatcher.BeginInvoke(
                priority,
                (Action)(
                () =>
                {
                    try
                    {
                        action();
                    }
                    catch (Exception ex)
                    {
                        //Log error here!
                    }
                })
            );
        }
    }
}

回答by Sankalp Saxena

This solution is a reduced version of the solution proposed by Tomá? Kafka(thanks to Tomas for describing his solution in detail)in this thread.

该解决方案是 Tomá 提出的解决方案的简化版本?Kafka(感谢 Tomas 详细描述了他的解决方案)在这个线程中。

In Tomas's solution he had1) DelegateCommand 2) CommandManagerHelper 3) DelegateCommandExtensions 4) NotifyPropertyChangedBaseExtensions 5) INotifyPropertyChangedWithRaise 6) ThreadTools

在 Tomas 的解决方案中,他有1) DelegateCommand 2) CommandManagerHelper 3) DelegateCommandExtensions 4) NotifyPropertyChangedBaseExtensions 5) INotifyPropertyChangedWithRaise 6) ThreadTools

This solution has1) DelegateCommand 2) DelegateCommandExtensions method and NotifyPropertyChangedBaseExtensions method in Delegate Command itself.

该解决方案在 Delegate Command 本身中1) DelegateCommand 2) DelegateCommandExtensions 方法和 NotifyPropertyChangedBaseExtensions 方法。

NoteSince our wpf application follows MVVM pattern and we handle commands at viewmodel level which executes in UI thread we don't need to get the reference to UI disptacher.

注意由于我们的 wpf 应用程序遵循 MVVM 模式,并且我们在视图模型级别处理在 UI 线程中执行的命令,因此我们不需要获取对 UI 调度程序的引用。

   using System;
   using System.Collections.Generic;
   using System.ComponentModel;
   using System.Linq.Expressions;
   using System.Reflection;
   using System.Windows.Input;
   namespace ExampleForDelegateCommand
   {
   public class DelegateCommand : ICommand
   {

    public Predicate<object> CanExecuteDelegate { get; set; }

    private List<INotifyPropertyChanged> propertiesToListenTo;
    private List<WeakReference> ControlEvent;

    public DelegateCommand()
    {
        ControlEvent= new List<WeakReference>();
    }

    public List<INotifyPropertyChanged> PropertiesToListenTo
    {
        get { return propertiesToListenTo; }
        set
        {
            propertiesToListenTo = value;
        }
    }

    private Action<object> executeDelegate;

    public Action<object> ExecuteDelegate
    {
        get { return executeDelegate; }
        set
        {
            executeDelegate = value;
            ListenForNotificationFrom((INotifyPropertyChanged)executeDelegate.Target);
        }
    }

    public static ICommand Create(Action<object> exec)
    {
        return new SimpleCommand { ExecuteDelegate = exec };
    }



    #region ICommand Members


    public bool CanExecute(object parameter)
    {
        if (CanExecuteDelegate != null)
            return CanExecuteDelegate(parameter);
        return true; // if there is no can execute default to true
    }

    public event EventHandler CanExecuteChanged
    {
        add
        {
            CommandManager.RequerySuggested += value;
            ControlEvent.Add(new WeakReference(value));
        }
        remove
        {
            CommandManager.RequerySuggested -= value;
            ControlEvent.Remove(ControlEvent.Find(r => ((EventHandler) r.Target) == value));
        }
    }

    public void Execute(object parameter)
    {
        if (ExecuteDelegate != null)
            ExecuteDelegate(parameter);
    }
     #endregion

    public void RaiseCanExecuteChanged()
    {
        if (ControlEvent != null && ControlEvent.Count > 0)
        {
            ControlEvent.ForEach(ce =>
                                     {
                                         if(ce.Target!=null)
                                         ((EventHandler) (ce.Target)).Invoke(null, EventArgs.Empty);
                                     });
        }
    }



    public DelegateCommand ListenOn<TObservedType, TPropertyType>(TObservedType viewModel, Expression<Func<TObservedType, TPropertyType>> propertyExpression) where TObservedType : INotifyPropertyChanged
    {
        string propertyName = GetPropertyName(propertyExpression);
        viewModel.PropertyChanged += (PropertyChangedEventHandler)((sender, e) =>
        {
            if (e.PropertyName == propertyName) RaiseCanExecuteChanged();
        });
        return this;
    }

    public void ListenForNotificationFrom<TObservedType>(TObservedType viewModel) where TObservedType : INotifyPropertyChanged
    {
        viewModel.PropertyChanged += (PropertyChangedEventHandler)((sender, e) =>
        {
           RaiseCanExecuteChanged();
        });
    }

    private string GetPropertyName<T, TProperty>(Expression<Func<T, TProperty>> expression) where T : INotifyPropertyChanged
    {
        var lambda = expression as LambdaExpression;
        MemberInfo memberInfo = GetmemberExpression(lambda).Member;
        return memberInfo.Name;
    }

    private MemberExpression GetmemberExpression(LambdaExpression lambda)
    {
        MemberExpression memberExpression;
        if (lambda.Body is UnaryExpression)
        {
            var unaryExpression = lambda.Body as UnaryExpression;
            memberExpression = unaryExpression.Operand as MemberExpression;
        }
        else
            memberExpression = lambda.Body as MemberExpression;
        return memberExpression;
    }

}}

Explanation of the solution:

解决方案说明:

Normally when we bind a UI element(Button)to the ICommand implementation the WPF Button registers for a Event "CanExecuteChanged" in ICommand implementation .If your Icommand implementation for "CanExecuteChanged" hook to the CommandManager's RequesySuggest event(read this article http://joshsmithonwpf.wordpress.com/2008/06/17/allowing-commandmanager-to-query-your-icommand-objects/) then when ever CommandManager detects conditions that might change the ability of a command to execute(changes like Focus shifts and some keyboard events) , CommandManager's RequerySuggested event occurs which in turn will cause Button'e delegate to be called since we hooked the buttos's delgate to CommandManager's RequerySuggested in the implementation of "CanExecuteChanged" in our DelegateCommand .

通常,当我们将 UI 元素(按钮)绑定到 ICommand 实现时,WPF 按钮会在 ICommand 实现中注册一个事件“CanExecuteChanged”。如果“CanExecuteChanged”的 Icommand 实现挂钩到 CommandManager 的 RequesySuggest 事件(阅读本文http:// joshsmithonwpf.wordpress.com/2008/06/17/allowing-commandmanager-to-query-your-icommand-objects/)然后当 CommandManager 检测到可能改变命令执行能力的条件时(例如焦点转移和一些键盘事件),CommandManager 的 RequerySuggested 事件发生,这反过来会导致 Button'e 委托被调用,因为我们在“CanExecuteChanged”的实现中将按钮的 delgate 挂钩到 CommandManager 的 RequerySuggested在我们的 DelegateCommand 中。

But the problem is that ComandManager is not able to always detect the changes. Hence the solution it to raise "CanExecuteChanged" when our command implementation(DelegateCommand) detects there is a change.Normally when we declare the delagate for ICommand's CanExecute in our viewmodel we bind to properties declared in our viewmodel and our ICommand implementation can listen for "propertychanged" events on these properties. Thats what the "ListenForNotificationFrom" method of the DelegateCommand does. In case the client code does not register for specific property changes the DelegateCommand by defaults listens to any property change on the view model where command is declared and defined.

但问题是 ComandManager 无法始终检测到更改。因此,当我们的命令实现(DelegateCommand)检测到有变化时,它会引发“CanExecuteChanged”的解决方案。通常,当我们在我们的视图模型中声明 ICommand 的 CanExecute 的延迟时,我们绑定到我们的视图模型中声明的属性,我们的 ICommand 实现可以监听“ propertychanged" 这些属性上的事件。这就是 DelegateCommand 的“ListenForNotificationFrom”方法所做的。如果客户端代码没有注册特定的属性更改,默认情况下 DelegateCommand 会侦听声明和定义命令的视图模型上的任何属性更改。

"ControlEvent" in DelegateCommand which is list of EventHandler that stores the Button's "CanExecuteChange EventHandler" is declared as weak reference to avoid memory leaks.

DelegateCommand 中的“ControlEvent”是存储按钮的“CanExecuteChange EventHandler”的 EventHandler 列表,被声明为弱引用以避免内存泄漏。

How will ViewModel use this DelegateCommand There are 2 ways to use it. (the second usage is more specific to the properties that you want the Command to listen to.

ViewModel 将如何使用这个 DelegateCommand 有 2 种使用方法。(第二种用法更特定于您希望 Command 侦听的属性。

delegateCommand = new DelegateCommand
                                      {
                                          ExecuteDelegate = Search,
                                          CanExecuteDelegate = (r) => !IsBusy
                                      };

          anotherDelegateCommand = new DelegateCommand
                                      {
                                          ExecuteDelegate = SearchOne,
                                          CanExecuteDelegate = (r) => !IsBusyOne
                                      }.ListenOn(this, n => n.IsBusyOne);

A detailed ViewModel

详细的 ViewModel

  public class ExampleViewModel
 {
   public SearchViewModelBase()
    {
        delegateCommand = new DelegateCommand
                                      {
                                          ExecuteDelegate = Search,
                                          CanExecuteDelegate = (r) => !IsBusy
                                      };

          anotherDelegateCommand = new DelegateCommand
                                      {
                                          ExecuteDelegate = SearchOne,
                                          CanExecuteDelegate = (r) => !IsBusyOne
                                      }.ListenOn(this, n => n.IsBusyOne);
  }
  private bool isBusy;
   public virtual bool IsBusy
    {
        get { return isBusy; }
        set
        {
            if (isBusy == value) return;
            isBusy = value;
            NotifyPropertyChanged(MethodBase.GetCurrentMethod());
        }
    }
    private bool isBusyOne;
     public virtual bool IsBusyOne
    {
        get { return isBusyOne; }
        set
        {
            if (isBusyOne == value) return;
            isBusyOne = value;
            NotifyPropertyChanged(MethodBase.GetCurrentMethod());
        }
    }


    private void Search(object obj)
    {
        IsBusy = true;
        new SearchService().Search(Callback);
    }  
    public void Callback(ServiceResponse response)
    {
        IsBusy = false;
    }  

    private void Search(object obj)
    {
        IsBusyOne = true;
        new SearchService().Search(CallbackOne);
    }  
    public void CallbackOne(ServiceResponse response)
    {
        IsBusyOne = false;
    }          
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    } 

    private void NotifyPropertyChanged(MethodBase methodBase)
    {
        string methodName = methodBase.Name;

        if (!methodName.StartsWith("set_"))
        {
            var ex = new ArgumentException("MethodBase must refer to a Property Setter method.");
            throw ex;
        }
        NotifyPropertyChanged(methodName.Substring(4));
    }

}

}

回答by nchaud

Tomas has a nice solution, but pls note there's a serious bug in that the CanExecute will not always fire when bound to a Button due to this :

Tomas 有一个很好的解决方案,但请注意有一个严重的错误,即 CanExecute 在绑定到 Button 时不会总是触发,因为:

// Call the handlers that we snapshotted
for (int i = 0; i < count; i++)
{
            EventHandler handler = callees[i];
            handler(null, EventArgs.Empty);
}

The 'null' parameter passed in causes issues with the CanExecuteChangedEventManager (used by the WPF Button class to listen to changes on any Command bound to it). Specifically, the CanExecuteChangedEventManager maintains a collection of weak events that need to be invoked to determine if the command Can-Execute() but this collection is keyed by the 'sender'.

传入的“null”参数会导致 CanExecuteChangedEventManager 出现问题(WPF Button 类使用它来侦听绑定到它的任何命令的更改)。具体来说, CanExecuteChangedEventManager 维护了一个弱事件集合,需要调用这些弱事件来确定命令 Can-Execute() 是否由“发送者”键控。

The fix is simple and works for me - change the signature to

修复很简单,对我有用 - 将签名更改为

internal static void CallWeakReferenceHandlers(ICommand sender, List<WeakReference> handlers)
{
....
           handler(sender, EventArgs.Empty);
 }

Sorry I haven't described it too well - in a bit of a rush to catch up with my dev now after taking a few hours to figure this out !

抱歉,我没有把它描述得太好 - 在花了几个小时弄清楚这个之后,现在有点急于赶上我的开发人员!

回答by Wouter

I would suggest looking into ReactiveUIand specifically at the ICommand implementation it provides, ReactiveCommand. It uses a different approach than DelegateCommand/RelayCommand which are implemented with delegates for CanExecute that must be actively evaluated. ReactiveCommand's value for CanExecute is pushed using IObservables.

我建议研究ReactiveUI,特别是它提供的 ICommand 实现ReactiveCommand。它使用与 DelegateCommand/RelayCommand 不同的方法,后者是通过必须主动评估的 CanExecute 委托实现的。ReactiveCommand 的 CanExecute 值是使用 IObservables 推送的。

回答by Lightman

is there a way I can make CommandManager.InvalidateRequerySuggested() do it's job faster?

有没有办法让 CommandManager.InvalidateRequerySuggested() 更快地完成工作?

Yes, there is way to make it work faster!

是的,有办法让它工作得更快!

  1. Implement Commandto keep / cache CanExecuteStatein a boolean variable.
  2. Implement RaiseCanExecuteChangedmethod to recalculate CanExecuteStateand if it really changed to raise CanExecuteChangedevent.
  3. Implement CanExecutemethod to simply return CanExecuteState.
  4. When InvalidateRequerySuggestedmethod is invoked Commandsubscribers will only read CanExecuteStatevariable by invoking CanExecutemethod and check if it changed or not. That's almost zero overhead. All Commandswill be disabled / enabled almost the same time.
  5. All work will be done in RaiseCanExecuteChangedmethod that will be called only once for a Commandand only for a limited set of Commands.
  1. 实现Command将/缓存CanExecuteState在布尔变量中。
  2. 实现RaiseCanExecuteChanged方法来重新计算CanExecuteState,如果它真的改变了引发CanExecuteChanged事件。
  3. 实现CanExecute方法以简单地返回CanExecuteState
  4. InvalidateRequerySuggested方法被调用时,Command订阅者只会CanExecuteState通过调用CanExecute方法读取变量并检查它是否改变。这几乎是零开销。所有这些Commands都将几乎同时被禁用/启用。
  5. 所有工作都将在RaiseCanExecuteChanged方法中完成,该方法只为 a 调用一次,Command并且只为有限的Commands.

回答by Angry Chipmonk

Try writing your own binding that calls your RaiseCanExecuteChanged() within converts? it is easier

尝试编写自己的绑定,在转换中调用 RaiseCanExecuteChanged()?更容易

回答by Angry Chipmonk

Just to clarify:

只是为了澄清:

  1. You want to fire an update of CanExecutewhen Command property changed
  2. Create your own binding class that detect changes in the Command propertyand then calls RaiseCanExecuteChanged()
  3. Use this binding in CommandParameter
  1. 您想触发更新CanExecute时间Command property changed
  2. 创建您自己的绑定类,用于检测 中的更改Command property,然后调用RaiseCanExecuteChanged()
  3. 使用此绑定 CommandParameter

Worked for me.

对我来说有效。