7

可以像这样直接绑定按钮操作:

var set = this.CreateBindingSet<...
set.Bind(button).To(x => x.Go);

但是,例如,关于 UITapGestureRecognizer 是什么。我应该如何以如此优雅的方式绑定它(它是点击动作)?

谢谢!

4

2 回答 2

20

仅供参考。较新版本的 MvvMcross 包含一个开箱即用的 UIView 方法扩展(请参阅 MvxTapGestureRecognizerBehaviour),您可以使用它来绑定点击手势:

using Cirrious.MvvmCross.Binding.Touch.Views.Gestures;

// in this case "Photo" is an MvxImageView
set.Bind(Photo.Tap()).For(tap => tap.Command).To("OpenImage");
于 2015-11-01T12:40:28.243 回答
17

如果你愿意,你可以自己添加。

例如类似的东西

  public class TapBehaviour
  {
      public ICommand Command { get;set; }

      public TapBehaviour(UIView view)
      {
          var tap = new UITapGestureRecognizer(() => 
          {
              var command = Command;
              if (command != null)
                   command.Execute(null);
          });
          view.AddGestureRecognizer(tap);
      }
  }

  public static class BehaviourExtensions
  {
      public static TapBehaviour Tap(this UIView view)
      {
          return new TapBehaviour(view);
      }
  }

  // binding
  set.Bind(label.Tap()).For(tap => tap.Command).To(x => x.Go);

我认为这会起作用 - 但这是在这里编码!


高级> 如果您愿意,您还可以For(tap => tap.Command)通过为 TapBehaviour 注册默认绑定属性来消除对部件的需求 - 执行此覆盖Setup.FillBindingNames并使用:

  registry.AddOrOverwrite(typeof (TapBehaviour), "Command");

在此之后,绑定可以是:

  set.Bind(label.Tap()).To(x => x.Go);

于 2013-06-06T14:28:05.807 回答