MvvmCross Droid MvxListView与搜索EditText?

Avr*_*hom 3 android listview mvvmcross xamarin

在MvvmCross中,是否可以在顶部使用带有Search EditText的Android MvxListView?如何?

小智 6

在View.axml中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        local:MvxBind="Text SearchString" />
    <Mvx.MvxListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        local:MvxBind="ItemsSource FilteredList" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

非常简单,EditText是您的搜索查询框,其下方的列表将是您的筛选列表.

在ViewModel.cs中:

public class FirstViewModel 
        : MvxViewModel
    {
        public FirstViewModel()
        {
            _filteredList = _completeList;
        }


        private string _searchString;
        public string SearchString
        {
            get { return _searchString; }
            set
            {
                _searchString = value;
                if (string.IsNullOrEmpty(value))
                {
                    _filteredList = _completeList;
                }
                else
                {
                    _filteredList = _completeList.Where(o => o == value).ToList();
                }
                RaisePropertyChanged(() => SearchString);
                RaisePropertyChanged(() => FilteredList);
            }
        }


        private List<string> _completeList = new List<string>() { "a", "b", "c", "d", "e" };
        private List<string> _filteredList;
        public List<string> FilteredList
        {
            get { return _filteredList; }
        }
    }
Run Code Online (Sandbox Code Playgroud)

这里的ViewModel从EditText接收SearchString,然后使用Linq过滤完整列表.然后,它会为筛选的List获取筛选列表和RaisesPropertyChanged.