【问题标题】:Call RelayCommand<T> with CanExecute Parameter使用 CanExecute 参数调用 RelayCommand<T>
【发布时间】:2023-04-03 18:50:01
【问题描述】:

如何通过传入 CanExecute 参数来调用 RelayCommand。该对象被绑定为视图中的命令参数。我有以下一段代码,上面写着“Delegate Func 不接受 0 个参数”。请帮忙。 OnPrintAllChartsInCurrentView 和 IsPrintAndExportEnabled 的定义如下。

     RelayCommand m_PrintAllChartsCommand;
                public ICommand PrintAllChartsCommand
                {
                    get
                    {
                        return m_PrintAllChartsCommand ?? (m_PrintAllChartsCommand = new RelayCommand<object>(
                            OnPrintAllChartsInCurrentView,
                            () => IsPrintAndExportEnabled() //This line there is a compiler error
                            ));
                    }
                }

private void OnPrintAllChartsInCurrentView(object obj)
        {
//do something. The obj is bound as command parameter from the view.
        }

    private bool IsPrintAndExportEnabled()
    {
    //I will do some operation here and change to return true or false later
    return false;
    }

这是我试图调用的 RelayCommand 类

namespace GalaSoft.MvvmLight.Command
{

    public class RelayCommand<T> : ICommand
    {
        //
        // Summary:
        //     Initializes a new instance of the RelayCommand class that can always execute.
        //
        // Parameters:
        //   execute:
        //     The execution logic.
        //
        // Exceptions:
        //   T:System.ArgumentNullException:
        //     If the execute argument is null.
        public RelayCommand(Action<T> execute);
        //
        // Summary:
        //     Initializes a new instance of the RelayCommand class.
        //
        // Parameters:
        //   execute:
        //     The execution logic.
        //
        //   canExecute:
        //     The execution status logic.
        //
        // Exceptions:
        //   T:System.ArgumentNullException:
        //     If the execute argument is null.
        public RelayCommand(Action<T> execute, Func<T, bool> canExecute);

        //
        // Summary:
        //     Occurs when changes occur that affect whether the command should execute.
        public event EventHandler CanExecuteChanged;

        //
        // Summary:
        //     Defines the method that determines whether the command can execute in its current
        //     state.
        //
        // Parameters:
        //   parameter:
        //     Data used by the command. If the command does not require data to be passed,
        //     this object can be set to a null reference
        //
        // Returns:
        //     true if this command can be executed; otherwise, false.
        public bool CanExecute(object parameter);
        //
        // Summary:
        //     Defines the method to be called when the command is invoked.
        //
        // Parameters:
        //   parameter:
        //     Data used by the command. If the command does not require data to be passed,
        //     this object can be set to a null reference
        public virtual void Execute(object parameter);
        //
        // Summary:
        //     Raises the GalaSoft.MvvmLight.Command.RelayCommand`1.CanExecuteChanged event.
        public void RaiseCanExecuteChanged();
    }
}

【问题讨论】:

  • Execute 和 CanExecute 必须具有相同的签名和参数....缺少对象:param => { return IsPrintAndExportEnabled(param); }

标签: c# wpf mvvm


【解决方案1】:

如果您使用 T 类型的命令参数(您将其声明为对象),则 RelayCommand CanExecute 需要 T 类型的单个参数并返回 bool。您正在传入一个匿名函数,它不接受任何参数并返回一个布尔值。你可以简单地替换

() => IsPrintAndExportEnabled();

arg => { return IsPrintAndExportEnabled(); }

如果您不打算对传递给 CanExecute 的对象执行任何操作。

如果你不需要命令参数,那么你不需要将你的 RelayCommand 声明为

RealyCommand<object>(execute, canExecute);

它可以简单地

RelayCommand(execute, canExecute);

在这种情况下,Execute 将不带参数并返回 void,而 CanExecute 将不带参数并返回布尔值。

【讨论】:

    【解决方案2】:

    应该是这样的。请注意,RelayCommand canExecute 参数是Func&lt;T,bool&gt;,这意味着您传递具有相同签名的方法(如下所示)。有关Func&lt;T,TResult&gt; 的更多信息,请参阅this

     RelayCommand m_PrintAllChartsCommand;
     public ICommand PrintAllChartsCommand
     {
           get
           {
                 return m_PrintAllChartsCommand ?? (m_PrintAllChartsCommand = new RelayCommand<object>(
                            OnPrintAllChartsInCurrentView,
                            IsPrintAndExportEnabled
                            ));
            }
      }
    
     private void OnPrintAllChartsInCurrentView(object arg)
     {
    
     }
    
     private bool IsPrintAndExportEnabled(object arg)
     {
          return false;
     }
    

    【讨论】:

      【解决方案3】:

      这对我有用。以防万一它对任何人有帮助。先看RelayCommand,我声明的方式是错误的。应该是RelayCommand&lt;object&gt; 这是完整的声明和用法。

         RelayCommand<object> m_PrintAllChartsCommand;
              public ICommand PrintAllChartsCommand
              {
                  get
                  {
      
                      return m_PrintAllChartsCommand ?? (m_PrintAllChartsCommand = new RelayCommand<object>(
                          OnPrintAllChartsInCurrentView, IsPrintAndExportEnabled));
      
                  }
              }
      private void OnPrintAllChartsInCurrentView(object arg)
       {
      
       }
      
       private bool IsPrintAndExportEnabled(object arg)
       {
       }
      

      【讨论】:

        【解决方案4】:

        这对我有用(IsRealShunter 是一个布尔属性):

        RaiseAlarmClickCommand = new RelayCommand<Guid>(RaiseAlarmClick, x => IsRealShunter);
        

        RaiseAlarmClick 是一个方法:

        private void RaiseAlarmClick(Guid idPrinter) {...}
        

        一个小提示,如果您的视图模型中的某些内容得到更新,并且您想确保您的 RelayCommand 反映更改,请从发生更改的地方调用它:

        RaiseAlarmClickCommand.RaiseCanExecuteChanged();
        

        【讨论】:

          猜你喜欢
          • 2011-01-19
          • 1970-01-01
          • 2013-02-10
          • 2022-12-18
          • 2018-06-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多