MvvmCross: GestureRecognized binding to ViewModel action
Asked Answered
U

2

7

There is such ability to bind buttons actions directly like this:

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

but what's about UITapGestureRecognizer, for instance. How should I bind it (it's tap action) in such elegant way?

Thank you!

Universal answered 6/6, 2013 at 11:30 Comment(0)
P
18

You could add this yourself if you wanted to.

e.g. something like

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

I think that would work - but this is coding live here!


Advanced> If you wanted to, you could also remove the need for the For(tap => tap.Command) part by registering a default binding property for TapBehaviour - to do this override Setup.FillBindingNames and use:

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

After this, then the binding could be:

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

Perigordian answered 6/6, 2013 at 14:28 Comment(3)
Thank you a lot! Despite of tiny mistypings, everything works like a charm!Universal
Updated. It was a trick to change only 4 characters (due to stackoverflow restrictions) though. he heUniversal
I get null as a value in the setter of TapBehaviour.Command - why would that be?Mogul
P
21

Just for reference. Newer version of MvvMcross includes a UIView method extension (see MvxTapGestureRecognizerBehaviour) out of the box that you can use to bind the tap gesture:

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

// in this case "Photo" is an MvxImageView
set.Bind(Photo.Tap()).For(tap => tap.Command).To("OpenImage");
Parnell answered 1/11, 2015 at 12:40 Comment(2)
Thanks for the reference. I've added a gesture recognizer for swipe: github.com/MvvmCross/MvvmCross/pull/1266Roderick
This is amazeballs. Not sure how I didn't know about these extensions. Saved me so much work binding to a tap on a UIStackview.Thereupon
P
18

You could add this yourself if you wanted to.

e.g. something like

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

I think that would work - but this is coding live here!


Advanced> If you wanted to, you could also remove the need for the For(tap => tap.Command) part by registering a default binding property for TapBehaviour - to do this override Setup.FillBindingNames and use:

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

After this, then the binding could be:

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

Perigordian answered 6/6, 2013 at 14:28 Comment(3)
Thank you a lot! Despite of tiny mistypings, everything works like a charm!Universal
Updated. It was a trick to change only 4 characters (due to stackoverflow restrictions) though. he heUniversal
I get null as a value in the setter of TapBehaviour.Command - why would that be?Mogul

© 2022 - 2024 — McMap. All rights reserved.