【发布时间】:2017-03-21 21:05:40
【问题描述】:
我无法使用 Fluent API 将 MvxRecyclerView(或其适配器)中的 ItemClick 绑定到我的 ViewModel 上的命令。如果我将 ItemsSource 和 ItemClick 都放在 XML 中,它会起作用,所以我对这种解决方案不感兴趣。
我使用这篇文章作为一个很好的指导方针 (How to use the MvvmCross fluent API to bind a RecyclerView item's TextView to a property of its ViewModel on Android?),所有这些都有效,除了我无法将 MvxRecyclerView(或适配器)上的 ItemClick 绑定到 MainViewModel 的命令,它将带我到下一个片段(ItemsSource像魅力一样工作,但它是属性而不是命令!)。
为简洁起见,我不会复制原始帖子 (How to use the MvvmCross fluent API to bind a RecyclerView item's TextView to a property of its ViewModel on Android?) 中的代码,因此假设该帖子中的 MainViewModel 已通过命令 ShowItemCommand 进行了增强:
public class MainViewModel : MvxViewModel
{
private IEnumerable<ViewModelItem> _viewModelItems;
public IEnumerable<ViewModelItem> ViewModelItems
{
get { return _viewModelItems; }
set { SetProperty(ref _viewModelItems, value); }
}
public MvxCommand<ViewModelItem> ShowItemCommand
{
get
{
return new MvxCommand<ViewModelItem>(selectedItem =>
{
ShowViewModel<ViewModelItem>
(new { itemId = selectedItem.Id });
});
}
}
}
其他所有内容都已按照引用的帖子实施。
所以现在,除了 ItemsSource 之外,我还想将 MvxRecyclerView(或 Adapter)上的 ItemClick 连接到命令。这些可以互换的原因是 MvxRecyclerView 只是将这些命令中继到适配器。
显然,这应该可以工作......但它没有:
adapter.ItemClick = ViewModel.ShowItemCommand;
这也不起作用:
set.Bind(recyclerView).For(v => v.ItemClick).To(vm => vm.ShowItemCommand);
【问题讨论】:
标签: android xamarin binding xamarin.android mvvmcross