To pass a dynamic command parameters to the command in the view model you could create a new class e.g. like this DynamicCommandParameterValueConverter
:
/// <summary>
/// This class is inspired by MvvmCross MvxCommandParameterValueConverter,
/// but because we need dynamic command parameters, we need to implement our own combined with a CustomMvxWrappingCommand.
/// </summary>
/// <typeparam name="T">The type of the 'selected item' for the command</typeparam>
/// <typeparam name="TResult">The returned result that is used as input for the command.</typeparam>
public class DynamicCommandParameterValueConverter<T, TResult> : MvxValueConverter<ICommand, ICommand>
{
private readonly Func<T, TResult> commandParameterExpression;
public DynamicCommandParameterValueConverter(Func<T, TResult> commandParameterExpression)
{
this.commandParameterExpression = commandParameterExpression;
}
protected override ICommand Convert(ICommand value, Type targetType, object parameter, CultureInfo culture)
{
return new CustomMvxWrappingCommand<T, TResult>(value, commandParameterExpression);
}
}
The CustomMvxWrappingCommand
takes a Func
as argument, and is later invoked and passed into the commands CanExecute/Execute
method. Here is a snippet of how part of that class could look like:
public void Execute(object parameter)
{
if (wrapped == null)
{
return;
}
if (parameter != null)
{
Mvx.Warning("Non-null parameter overridden in MvxWrappingCommand");
}
wrapped.Execute(commandParameterOverride((T)parameter));
}
You could modify the MvxWrappingCommand
class from Mvx to implement the above example.
The use of it all:
set.Bind(myControl).For(control => control.ItemClick).To(vm => vm.MyCommand).WithConversion(
new DynamicCommandParameterValueConverter<MyModel, string>((MyModel item) =>
{
// Do custom logic before parsing the item..
}));