将视图绑定到ICommand.CanExecute [英] Binding views to ICommand.CanExecute

查看:75
本文介绍了将视图绑定到ICommand.CanExecute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将视图属性绑定到ICommand.CanExecute?

Is it somehow possible to bind view properties to ICommand.CanExecute?

例如,我希望能够在触摸视图中执行以下操作:

I'd for example like to be able to do something like this in a touch view:

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

我已经阅读了如何在Mvvmcross中使用CanExecute ,但是不幸的是,它跳过了问题,而只是提出了另一种实现方式.

I've already read How to use CanExecute with Mvvmcross, but unfortunately it skips the questions and instead just proposes another implementation.

推荐答案

一种方法是使用从UIButton继承的自定义按钮.

One way of doing this is to use your own custom button inheriting from UIButton.

对于Android,我已经实现了此实现-它是:

For Android, I've got an implementation of this to hand - it is:

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);
    }
}

,这可以绑定为:

<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-但我恐怕没有此代码此刻我.

For iOS, I'd expect the same type of technique to work... inheriting from a UIButton and using TouchUpInside instead of Click - but I'm afraid I don't have this code with me at the moment.

这篇关于将视图绑定到ICommand.CanExecute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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