mvvmcross自定义绑定到eventhandler

时间:2013-02-17 22:35:21

标签: event-handling mvvmcross custom-binding

我正在尝试在视图上实现LongClick功能,并阅读以下提供了一些信息       mvvmcross touch command binding in android

在代码中搜索IMvxCommand失败,所以假设这可能已经过时了?所以我尝试了最好的努力,但无法获得任何LongClick功能 - 可能是由于对C#和事件处理程序的了解有限。我实现了以下内容,但不确定MvxRelayCommand的用法。

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

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

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

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

<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"/>

然而,当我在模拟器上运行代码并且在listitem上的LongClick鼠标按钮没有太多发生时。 是否需要在视图

中实现以下内容
public event EventHandler<View.LongClickEventArgs> LongClick;

任何帮助/指示赞赏。

1 个答案:

答案 0 :(得分:1)

对于列表,vNext MvxBindableListView支持ItemLongClick一段时间 - 请参阅

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

请注意,此绑定挂钩到ListView的ItemLongClick而不是LongClick

在你的axml中使用它,你应该能够做到:

<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"/>

如果这不起作用,请发布有关Github问题的错误报告。


如果你想在通用(非列表)视图上进行自定义绑定,那么你的代码需要切换到ICommand而不是IMvxCommand,你也无法真正传递Item参数 - 所以你要需要在ViewModel上使用MvxRelayCommand。

我已在问题列表中添加了View-level LongClick支持 - https://github.com/slodge/MvvmCross/issues/165

但是对于ListView,它可能是您实际感兴趣的ItemLongClick