MvvmCross绑定iOS手势

时间:2013-11-04 15:19:20

标签: xamarin.ios mvvmcross

我正在寻找一种方法,如何将ios手势如UILongPressGestureRecognizer绑定到MvvmCross中的ICommand或MvxCommand,谢谢。

PS:我找到了一个例子here,但我无法弄清楚如何做到这一点。

1 个答案:

答案 0 :(得分:0)

从您找到的示例和当前的MVVM Cross源代码中,我执行了以下操作

public static class MvxBehaviourExtensions
{
    public static MvxLongPressGestureRecognizerBehaviour LongPress(this UIView view)
    {
        var toReturn = new MvxLongPressGestureRecognizerBehaviour(view);
        return toReturn;
    }
}

public class MvxLongPressGestureRecognizerBehaviour
    : MvxGestureRecognizerBehavior<UILongPressGestureRecognizer>
{
    protected override void HandleGesture(UILongPressGestureRecognizer gesture)
    {
        // Long press recognizer fires continuously. This will ensure we fire
        // the command only once. Fire as soon as gesture is recognized as
        // a long press.
        if (gesture.State == UIGestureRecognizerState.Began)
        {
            FireCommand();
        }
    }

    public MvxLongPressGestureRecognizerBehaviour(UIView target)
    {
        var lp = new UILongPressGestureRecognizer(HandleGesture);

        AddGestureRecognizer(target, lp);
    }
}

并绑定

set.Bind(this.LongPress()).For(lp => lp.Command).To(c => c.DoTheStuffCommand);