MvvMCross 带参数的绑定命令(在 C# 代码中) [英] MvvMCross bind command with parameter (in C# code)

查看:29
本文介绍了MvvMCross 带参数的绑定命令(在 C# 代码中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过指定命令参数将命令绑定到 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); 

我在哪里可以指定将传递给命令的参数(真/假)?

Where can I specify the parameter (true/false) that will be passed to the command?

推荐答案

Android 和 iOS 按钮不像 Windows 按钮那样具有 CommandParameter 属性.

Android and iOS buttons don't have CommandParameter properties in the same way that Windows ones do.

然而,MvvmCross 最近确实引入了一种通过值转换器引入 CommandParameter 绑定的方法 - 请参阅 http://slodge.blogspot.co.uk/2013/06/commandparameter-binding.html

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

这个绑定应该是这样的:

This binding should work as:

 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:

  1. 在您的 Setup.cs 中添加此手动值转换器注册

  1. 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()
    );
}

  • 使用自 3.0.8.1 以来上传的 beta nuget 包之一(将 nuget 设置为包含预发布版本以查看这些包).

  • Or use one of the beta nuget packages uploaded since 3.0.8.1 (set nuget to include prereleases to see these packages).

    或者自己构建源代码

    这篇关于MvvMCross 带参数的绑定命令(在 C# 代码中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆