은 새로운 클래스를 만들 수 있습니다 뷰 모델의 명령에 동적 명령 매개 변수를 전달하려면 예 이 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);
}
}
CustomMvxWrappingCommand
는 인수로 Func
소요되며, 나중에 호출 및 명령 CanExecute/Execute
방법으로 전달됩니다. 당신은 위의 예를 구현하는 MVX에서 MvxWrappingCommand
클래스를 수정할 수
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));
}
: 여기에 클래스의 일부처럼 보일 수있는 방법의 조각이다.
모두의 사용 :
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..
}));
이 답변에 새로운 업데이트가 있습니까? 내 UIButton에 iOS에서 동적 명령 매개 변수를 사용하고 싶습니다. 예를 들어, 버튼을 클릭하면 내 UITextField 컨트롤 중 하나에 현재 텍스트가있는 내 명령이 호출되고 .Bind로 연결됩니다. – Michael