Có ai biết cách chuyển các tham số đến Command
bằng cách sử dụng CommandHandler
không? Giả sử tôi muốn chuyển giá trị mã hóa chuỗi cứng từ XAML. Tôi biết làm thế nào để vượt qua từ XAML, nhưng không phải làm thế nào để xử lý nó trong mã MVVM phía sau. Mã bên dưới hoạt động tốt nếu không cần phải chuyển bất kỳ thông số nào.Truyền tham số cho Lệnh 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();
}
}