Question

I am trying to implement LongClick functionality on a view and read the following which provided some info mvvmcross touch command binding in android

Searched unsuccessfully for IMvxCommand within the code so assume this may be outdated? So I attempted a best effort but cannot get any LongClick functionality - probably due to limited knowledge of C# and eventhandlers. I implemented the following but was not sure of the MvxRelayCommand usage.

public class LongClickEventBinding: MvxBaseAndroidTargetBinding
{
private readonly View _view;
private MvxRelayCommand<JobJob> _command;

public LongClickEventBinding(View view)
{
    _view = view;
    _view.LongClick += ViewOnLongClick;
}

private void ViewOnLongClick(object sender, View.LongClickEventArgs eventArgs)
{
    if (_command != null)
    {
        _command.Execute();
    }
}

public override void SetValue(object value)
{
   _command = (MvxRelayCommand<JobJob>)value;
}

protected override void Dispose(bool isDisposing)
{
    if (isDisposing)
    {
        _view.LongClick -= ViewOnLongClick;
    }
    base.Dispose(isDisposing);
}

public override Type TargetType
{
   get { return typeof(MvxRelayCommand<JobJob>); }
}

public override MvxBindingMode DefaultMode
{
    get { return MvxBindingMode.OneWay; }
}
}

And

  protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
     {
     base.FillTargetFactories(registry);
     registry.RegisterFactory(new MvxCustomBindingFactory<View>("LongClick", view => new LongClickEventBinding(view)));
     }

And

  public ICommand JobSelectedCommand
     {
     get { return new MvxRelayCommand<JobJob>(NavigateToJobTasks); }
     }

  public void NavigateToJobTasks(JobJob jobJob)
     {
        RequestNavigate<JobTaskListViewModel>(new { key = jobJob.JobID });
     }

And

<Mvx.MvxBindableListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
local:MvxBind="{'ItemsSource':{'Path':'GroupedList'},'LongClick':{'Path':'JobSelectedCommand'}}"    
local:MvxItemTemplate="@layout/listitem_job_old"/>

However when I run code on the emulator and LongClick mouse button on listitem not much happens. Does the following need to be implemented in the View

public event EventHandler<View.LongClickEventArgs> LongClick;

Any help / pointers appreciated.

Was it helpful?

Solution

For lists, vNext MvxBindableListView has supported ItemLongClick for a while anyway - see

https://github.com/slodge/MvvmCross/blob/vnext/Cirrious/Cirrious.MvvmCross.Binding.Droid/Views/MvxBindableListView.cs#L77

Note that this binding hooks into the ListView's ItemLongClick rather than into LongClick

Using this in your axml, you should be able to just do:

<Mvx.MvxBindableListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
local:MvxBind="{'ItemsSource':{'Path':'GroupedList'},'ItemLongClick':{'Path':'JobSelectedCommand'}}"    
local:MvxItemTemplate="@layout/listitem_job_old"/>

If this doesn't work then please fire a bug report on Github issues.


If you wanted to do your custom binding on a generic (non list) View, then your code would need to switch to ICommand instead of IMvxCommand, and you also couldn't really pass in the Item argument - so you'd need to just use MvxRelayCommand on the ViewModel.

I've added View-level LongClick support to the issues list - https://github.com/slodge/MvvmCross/issues/165

But for a ListView it is probably the ItemLongClick you are actually interested in

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top