问题描述
如何在MvvMCross(Xamarin.iOS)的代码中将命令绑定到指定命令参数的按钮?
How can I bind a command to a button in code in MvvMCross (Xamarin.iOS) with specifying a command parameter?
// command definition
public MvxCommand SaveDealerDataCommand
{
get { return new MvxCommand<bool>(DoSaveDealerDataAction); }
}
public void DoSaveDealerDataAction(bool show)
{
//...
}
// binding
bindingset.Bind(saveButton).To(vm => vm.SaveDealerDataCommand);
在哪里可以指定将传递给命令的参数(true / false) p>
Where can I specify the parameter (true/false) that will be passed to the command?
推荐答案
Android和iOS按钮没有以同样的方式拥有 CommandParameter
属性
Android and iOS buttons don't have CommandParameter
properties in the same way that Windows ones do.
然而,MvvmCross最近介绍了一种通过Value Converters引入 CommandParameter
绑定的方法 -
However, MvvmCross did recently introduce a way to introduce CommandParameter
bindings via Value Converters - see http://slodge.blogspot.co.uk/2013/06/commandparameter-binding.html
此绑定应该作为:
bindingset
.Bind(saveButton)
.To(vm => vm.SaveDealerDataCommand)
.WithConversion("CommandParameter", true);
或:
bindingset
.Bind(saveButton)
.To(vm => vm.SaveDealerDataCommand)
.WithConversion(new MvxCommandParameterValueConverter(), true);
请注意,此 CommandParameter
绑定完全在3.0.8.1包中是稳定的nuget版本,所以要使这项工作你可能需要:
Note that this CommandParameter
binding isn't completely in the 3.0.8.1 package which is the stable nuget release, so to make this work you may need to either:
-
在您的Setup.cs中添加此手动值转换器注册
Add this manual value converter registration in your Setup.cs
protected override void FillValueConverters(IMvxValueConverterRegistry registry)
{
base.FillValueConverters(registry);
registry.AddOrOverwrite(
"CommandParameter",
new Cirrious.MvvmCross.Binding.MvxCommandParameterValueConverter()
);
}
或
或自己创建源代码
这篇关于带参数的MvvMCross bind命令(在C#代码中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!