3

this is with regard to the following question on stackoverflow. I am trying to have a touch command for a button as described here but nothing happens. mvvmcross touch command binding in android

<Button
xmlns:local="http://schemas.android.com/apk/res/Test.UI.Droid"
android:text="Office"
android:layout_column="0"
android:id="@+id/imageButton1" 
local:MvxBind="{'Touch':{'Path':'ItemClickCommand'}}"/>  


public IMvxCommand ItemClickCommand
{
get
{
return new MvxRelayCommand(() => this.RequestNavigate<Tests.OfficeViewModel>(true));
}
}  

What am I doing wrong in the above code, why does it not fire.

Community
  • 1
  • 1
tribal
  • 219
  • 3
  • 14

2 Answers2

2

There's no binding in place for a Touch at present.

public event EventHandler<View.TouchEventArgs> Touch

If you wanted to add one, then search on StackOverflow for how to setup a new binding - e.g. the answer in mvvmcross touch command binding in android gives quite a full example.


However... for most button presses, you can probably just use:

public event EventHandler Click

Because it's an EventHandler rather than an EventHandler<TCustom> then this binds automagically.

i.e.:

<Button
xmlns:local="http://schemas.android.com/apk/res/Test.UI.Droid"
android:text="Office"
android:layout_column="0"
android:id="@+id/imageButton1" 
local:MvxBind="{'Click':{'Path':'ItemClickCommand'}}"/>  

should work - and it's probably what you are looking for - you want to respond to the Click of a button, not just a Touch?


To debug problems with binding it's worth looking at the MvxBindingTrace output - normally it will tell you when it can't bind to things - and if it doesn't, then please log bugs on github issues :)

Community
  • 1
  • 1
Stuart
  • 66,722
  • 7
  • 114
  • 165
0

I had the same problem with the Android. This is Stuart's answer and it worked. I just wanted to put it here for more convenient find for the android. Just put this class in your project and the command bindings will start to work on the device in release mode:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace BestSellers.Droid
{
  // things in this class are only required in order to prevent the linker   overoptimising!
class LinkerIncludePlease
{
    private void IncludeVisibility(View widget)
    {
        widget.Visibility = widget.Visibility + 1;           
    }

    private void IncludeClick(View widget)
    {
        widget.Click += (s,e) => {};
    }

    private void IncludeRelativeLayout(RelativeLayout relativeLayout)
    {
        relativeLayout.Click += (s, e) => { };
    }
}

}

mike gold
  • 1,551
  • 12
  • 12