【问题标题】:MvvmCross binding iOS GesturesMvvmCross 绑定 iOS 手势
【发布时间】:2013-11-04 15:19:19
【问题描述】:

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

PS:我找到了一个示例 here,但我不知道该怎么做。

【问题讨论】:

  • 这个例子的哪一点你不明白?如果您将Tap 替换为LongPress,那么有什么作用/不起作用 - 是否存在编译错误?还是运行时异常?
  • 我添加了类 LongPressBehaviour 和 BehaviourExtensions 但是当我制作 label.LongPress();它不认识它。

标签: xamarin.ios mvvmcross


【解决方案1】:

根据您找到的示例和当前的 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    相关资源
    最近更新 更多