Pregunta

Estoy aprendiendo Silverlight y mirando a MVVM y dominante.

Ok, por lo que he visto la implementación básica RelayCommand:

public class RelayCommand : ICommand
{
    private readonly Action _handler;
    private bool _isEnabled;

    public RelayCommand(Action handler)
    {
        _handler = handler;
    }

    public bool IsEnabled
    {
        get { return _isEnabled; }
        set
        {
            if (value != _isEnabled)
            {
                _isEnabled = value;
                if (CanExecuteChanged != null)
                {
                    CanExecuteChanged(this, EventArgs.Empty);
                }
            }
        }
    }

    public bool CanExecute(object parameter)
    {
        return IsEnabled;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        _handler();
    }
}

¿Cómo puedo pasar un parámetro abajo con un comando con esto?

he visto que se puede pasar un CommandParameter como esto:

<Button Command="{Binding SomeCommand}" CommandParameter="{Binding SomeCommandParameter}" ... />

En mi modelo de vista, tengo que crear el comando, pero RelayCommand espera un delegado Action. ¿Puedo aplicar RelayCommand<T> usando Action<T> -? Si es así, ¿cómo lo hago y cómo lo uso

Puede alguien darme algunos ejemplos prácticos sobre CommandParameters con MVVM que no implican el uso de bibliotecas 3 ª parte (por ejemplo MVVM Light) como quiero entenderlo completamente antes de usar las bibliotecas existentes.

Gracias.

¿Fue útil?

Solución

public class Command : ICommand
{
    public event EventHandler CanExecuteChanged;

    Predicate<Object> _canExecute = null;
    Action<Object> _executeAction = null;

    public Command(Predicate<Object> canExecute, Action<object> executeAction)
    {
        _canExecute = canExecute;
        _executeAction = executeAction;
    }
    public bool CanExecute(object parameter)
    {
        if (_canExecute != null)
            return _canExecute(parameter);
        return true;
    }

    public void UpdateCanExecuteState()
    {
        if (CanExecuteChanged != null)
            CanExecuteChanged(this, new EventArgs());
    }

    public void Execute(object parameter)
    {
        if (_executeAction != null)
            _executeAction(parameter);
        UpdateCanExecuteState();
    }
}

es la clase base para los comandos

Y este es su propiedad de comandos en modelo de vista:

 private ICommand yourCommand; ....

 public ICommand YourCommand
    {
        get
        {
            if (yourCommand == null)
            {
                yourCommand = new Command(  //class above
                    p => true,    // predicate to check "CanExecute" e.g. my_var != null
                    p => yourCommandFunction(param1, param2));
            }
            return yourCommand;
        }
    }

en XAML enlace de conjunto de comandos a la propiedad como:

 <Button Command="{Binding Path=YourCommand}" .../>

Otros consejos

Tal vez este artículo se explica lo que estás buscando. También tuve el mismo problema hace unos minutos.

http://www.eggheadcafe.com/sample-code/SilverlightWPFandXAML/76e6b583-edb1-4e23-95f6-7ad8510c0f88/pass-command-parameter-to-relaycommand.aspx

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top