【问题标题】:mvvmcross custom binding to eventhandlermvvmcross 自定义绑定到事件处理程序
【发布时间】:2013-02-02 07:52:22
【问题描述】:

我正在尝试在视图上实现 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 上长按鼠标按钮时,并没有发生太多事情。 以下是否需要在View中实现

public event EventHandler<View.LongClickEventArgs> LongClick;

任何帮助/指针表示赞赏。

【问题讨论】:

    标签: event-handling mvvmcross custom-binding


    【解决方案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。

    我已将视图级 LongClick 支持添加到问题列表 - https://github.com/slodge/MvvmCross/issues/165

    但对于 ListView,它可能是您真正感兴趣的 ItemLongClick

    【讨论】:

    • 按指示工作。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多