2

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

PS:我在这里找到了一个例子,但我不知道该怎么做。

4

1 回答 1

0

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

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);
于 2016-01-14T21:01:49.127 回答