我有一个 MvxListview,我需要检索被单击的项目的索引值,以便我可以将它传递到即将到来的 ViewModel。
是否有针对此问题的 Mvvmcross 特定解决方案?是否有数据绑定(bind)来检索索引?
初始的 MvxListview 是使用以下布局从远程服务器生成的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res/Flashcards.Android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Mvx.MvxListview
android:layout_width="fill_parent"
android:layout_height="wrap_content"
local:MvxBind="ItemsSource TableData;ItemClick NavigateToItemCommand"
local:MvxItemTemplate="@layout/stackstable_item" />
</LinearLayout>
这是导航命令。
//Defined in Constructor
m_navigateToItemCommand = new MvxCommand(NavigateToItem);
...
public ICommand NavigateToItemCommand
{
get { return m_navigateToItemCommand; }
}
void NavigateToItem()
{
//TODO Retrieve ListView Index, Pass Index to new ViewModel.
ShowViewModel<StacksTableItemViewModel>(new
{
SelectedStackIndex = 0;
});
}
非常感谢任何帮助。谢谢
最佳答案
而不是使用 MvxCommand
, 你可以使用 MvxCommand<T>
用ItemClick
在 MvxListView
上
这会将元素传回给您:
private Cirrious.MvvmCross.ViewModels.MvxCommand<StacksTableItem> _itemSelectedCommand;
public System.Windows.Input.ICommand ItemSelectedCommand
{
get
{
_itemSelectedCommand = _itemSelectedCommand ?? new Cirrious.MvvmCross.ViewModels.MvxCommand<StacksTableItem>(DoSelectItem);
return _itemSelectedCommand;
}
}
private void DoSelectItem(StacksTableItem item)
{
ShowViewModel<StacksTableItemViewModel>(new { id = item.Id });
}
如果有帮助,在 https://github.com/slodge/MvvmCross-Tutorials/ 中有一些这样的例子- 例如inside Daily Dilbert ListViewModel.cs
关于mvvm - 单击时获取 MvxListItem 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17099409/