MvvmCross复选框绑定到命令android xml

Sly*_*Sly 5 user-interface android mvvmcross xamarin

是否可以绑定android复选框以在更改时执行命令?找不到一个例子

Ond*_*unc 17

标准方法是在viewmodel中简单地绑定bool类型的属性,并在此属性的setter中执行逻辑.您的绑定将如下所示:

local:MvxBind="Checked IsChecked"
Run Code Online (Sandbox Code Playgroud)

但是,如果您确实需要绑定到Command,您还可以绑定到Click事件:

local:MvxBind="Checked IsChecked; Click YourCommand;"
Run Code Online (Sandbox Code Playgroud)

视图模型:

private bool _isChecked;

public bool IsChecked
{
    get { return _isChecked; }
    set
    {
        _isChecked = value;
        RaisePropertyChanged(() => IsChecked);
    }
}

public ICommand YourCommand
{
    get
    {
        return new MvxCommand(() =>
        {
            var isChecked = IsChecked;
            //Now you can use isChecked variable
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,您没有在命令参数中接收复选框的值,因此无论如何都需要绑定到bool属性.此解决方案的另一个问题是您必须依赖于一个事实,即在命令之前调用属性的setter.

如果你真的需要使用bool参数命令,那么你绝对可以这样做.关于MvvmCross框架的一个很棒的事情是你可以随时扩展它的功能.在您的情况下,您需要为CheckBox实现自定义绑定.好的起点可能在这里:http ://slodge.blogspot.cz/2013/06/n28-custom-bindings-n1-days-of-mvvmcross.html

编辑:为了表明它是多么容易我尝试了一下用bool参数实现简单的命令绑定.(没有CanExecute检查).如果有人有兴趣,这里是代码.
绑定类:

public class CheckBoxChangedBinding
    : MvxAndroidTargetBinding
{
    private ICommand _command;

    protected CheckBox View
    {
        get { return (CheckBox) Target; }
    }

    public CheckBoxChangedBinding(CheckBox view)
        : base(view)
    {
        view.CheckedChange += CheckBoxOnCheckedChange;

    }

    private void CheckBoxOnCheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
    {
        if (_command == null)
            return;
        var checkBoxValue = e.IsChecked;
        _command.Execute(checkBoxValue);
    }


    protected override void SetValueImpl(object target, object value)
    {
        _command = value as ICommand;
    }

    public override MvxBindingMode DefaultMode
    {
        get { return MvxBindingMode.OneWay; }
    }

    public override Type TargetType
    {
        get { return typeof (ICommand); }
    }

    protected override void Dispose(bool isDisposing)
    {
        if (isDisposing)
        {
            var view = View;
            if (view != null)
            {
                view.CheckedChange -= CheckBoxOnCheckedChange;
            }
        }
        base.Dispose(isDisposing);
    }
}
Run Code Online (Sandbox Code Playgroud)

在Setup.cs中:

protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
    base.FillTargetFactories(registry);
    registry.RegisterCustomBindingFactory<CheckBox>("CheckedChanged",
        checkBox => new CheckBoxChangedBinding(checkBox));
}
Run Code Online (Sandbox Code Playgroud)

在你的布局中:

<CheckBox
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    local:MvxBind="CheckedChanged CheckBoxCheckedCommand" />
Run Code Online (Sandbox Code Playgroud)

最后是ViewModel:

public ICommand CheckBoxCheckedCommand
{
    get
    {
        return new MvxCommand<bool>(isChecked =>
        {
            var parameter = isChecked;
        });
    }
}
Run Code Online (Sandbox Code Playgroud)