简体   繁体   English

将命令与参数绑定

[英]Bind command with parameters

In my Xamarin iOS project, I want to bind a button to a ICommand with a parameter. 在我的Xamarin iOS项目中,我想使用参数将按钮绑定到ICommand。

View: 视图:

var set = this.CreateBindingSet<MyView, MyViewModel>();
set.Bind(Button1).To(vm => vm.EditCommand).WithConversion(new MvxCommandParameterValueConverter(), 1);
set.Apply();

ViewModel: ViewModel:

private readonly ICommand editCommand;
public MyViewModel()
{
   editCommand = new BaseMvxCommand<int>(DoEditPhoto);
}

public ICommand EditCommand { get { return editCommand; } }
private void DoEditPhoto(int imageNum)
{
    // enter code here
}

When I hit the button, I am not able to execute the DoEditPhoto() . 当我按下按钮时,我无法执行DoEditPhoto() Am I binding in a wrong way? 我绑定的方式有误吗? Can anyone help me out? 谁能帮我吗?

Yes your binding is technically not wrong. 是的,您的绑定在技术上没有错。 However, you don't need a converter to pass a parameter to a bound ICommand . 但是,您不需要转换器将参数传递给绑定的ICommand For this you can use CommandParameter in your chain instead: 为此,您可以在链中使用CommandParameter

set.Bind(Button1).To(vm => vm.EditCommand).CommandParameter(ViewModel.ImageNumber);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM