单击时获取MvxListItem索引

时间:2013-06-14 00:36:31

标签: mvvm mono xamarin.ios xamarin.android mvvmcross

我有一个MvxListview,我需要检索被点击的项目的索引值,以便我可以将其传递给即将到来的ViewModel。

是否有针对此的Mvvmcross特定解决方案?是否有数据绑定来检索索引?

使用以下布局从远程服务器生成初始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;
    });
}

非常感谢任何帮助。感谢

1 个答案:

答案 0 :(得分:2)

您可以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