mvvm - 单击时获取 MvxListItem 索引

标签 mvvm mono xamarin.ios xamarin.android mvvmcross

我有一个 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>ItemClickMvxListView

这会将元素传回给您:

    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/

相关文章:

iphone - 为什么不能重用任何UITableView单元格?

c# - Monotouch 中的 System.ComponentModel.BackgroundWorker - 正确用法?

ios - MonoTouch,更改表格 View 数据

c# - 标签可见性。折叠在空白内容WPF MVVM上

asp.net-mvc - 单声道 - 找不到 system.web.mvc

c# - 如何在 Mono 和多个平台上使用 X509Certificate2 正确验证 SSL 证书

linux - 如何提交到裸存储库?

c# - 如何将多个 View 的DataContext设置为ViewModel的一个实例

具有不同 ItemsSource 的 WPF MVVM DataGrid RowDetails

c# - 将 DataTable 绑定(bind)到 DataGrid。 WPF MVVM