有谁知道如何使用CommandHandler
将参数传递给Command
?假设我想从XAML传递字符串硬编码值。我知道如何从XAML传递,但不知道如何在MVVM代码中处理它。下面的代码工作正常,如果没有必要传递任何参数。传递参数给MVVM命令
public ICommand AttachmentChecked
{
get
{
return _attachmentChecked ?? (_attachmentChecked = new CommandHandler(() => ExecuteAttachmentChecked(), CanExecuteAttachmentChecked()));
}
}
private void ExecuteAttachmentChecked()
{
}
private bool CanExecuteAttachmentChecked()
{
return true;
}
CommandHandler:
public class CommandHandler : ICommand
{
private Action _action;
private bool _canExecute;
public CommandHandler(Action action, bool canExecute)
{
_action = action;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_action();
}
}