mvvmcross - 将 View 绑定(bind)到 ICommand.CanExecute

标签 mvvmcross

是否可以将 View 属性绑定(bind)到 ICommand.CanExecute?

例如,我希望能够在触摸 View 中执行类似的操作:

this
    .CreateBinding(SignInWithFacebookButton)
    .For(b => b.Enabled)
    .To((SignInViewModel vm) => vm.SignInWithFacebookCommand.CanExecute)
    .Apply();

我已经读过How to use CanExecute with Mvvmcross ,但不幸的是它跳过了问题,而只是提出了另一种实现。

最佳答案

实现此目的的一种方法是使用您自己的继承自 UIButton 的自定义按钮。

对于 Android,我手头上有一个实现 - 它是:

public class FullButton : Button
{
    protected FullButton(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
    {
        Click += OnClick;
    }

    public FullButton(Context context) : base(context)
    {
        Click += OnClick;
    }

    public FullButton(Context context, IAttributeSet attrs) : base(context, attrs)
    {
        Click += OnClick;
    }

    public FullButton(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
    {
        Click += OnClick;
    }

    private IDisposable _subscription;

    private object _commandParameter;
    public object CommandParameter
    {
        get { return _commandParameter; }
        set
        {
            _commandParameter = value;
            UpdateEnabled();
        }
    }

    private ICommand _command;
    public ICommand Command
    {
        get { return _command; }
        set
        {
            if (_subscription != null)
            {
                _subscription.Dispose();
                _subscription = null;
            }

            _command = value;

            if (_command != null)
            {

                var cec = typeof (ICommand).GetEvent("CanExecuteChanged");
                _subscription = cec.WeakSubscribe(_command, (s, e) =>
                    {
                        UpdateEnabled();
                    });
            }

            UpdateEnabled();
        }
    }

    private void OnClick(object sender, EventArgs eventArgs)
    {
        if (Command == null)
            return;

        if (Command.CanExecute(CommandParameter))
            Command.Execute(CommandParameter);
    }

    private void UpdateEnabled()
    {
        Enabled = ShouldBeEnabled();
    }

    private bool ShouldBeEnabled()
    {
        if (_command == null)
            return false;

        return _command.CanExecute(CommandParameter);
    }
}

这可以绑定(bind)为:

<FullButton
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Show Detail"
    local:MvxBind="Command ShowDetailCommand; CommandParameter CurrentItem" />

对于 iOS,我希望相同类型的技术能够工作...继承自 UIButton 并使用 TouchUpInside 而不是 Click > - 但恐怕我现在没有这个代码。

关于mvvmcross - 将 View 绑定(bind)到 ICommand.CanExecute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20370559/

相关文章:

c# - 单元测试 MvvmCross.Droid.View

inversion-of-control - MVVMCross 中特定于平台的 IoC

xamarin - 如何将单点触控 UI 元素的 'enabled' 属性绑定(bind)到 Mvvmcross 中的 View 模型 bool 值

android - MvvmCross - 将 EditText 设置为 void 不会更新 ViewModelProperty

binding - MvvMCross 带参数的绑定(bind)命令(在 C# 代码中)

ios - Xamarin、MVVM交叉和崩溃报告

xamarin - 升级到 MVVMCross 4.0 后 LinkAssemblies 失败

ios - 防止可见 UICollectionView 单元格闪烁

ios - MvvmCross:iOS 设备上的绑定(bind)警告

android - 有没有办法让 Mvx.MvxLinearLayout 尊重 android :layout_weight?