让 MvxAutoCompleteTextView 工作

Getting MvxAutoCompleteTextView to work

我正在使用 Xamarin 开发一个 android 应用程序,我为此使用了 MVVMCross。 现在我想使用 MvxAutoCompleteTextView 但我无法让它工作。

我做了一个样本,无法正确获取

下面是我的代码: 自动完成视图:

<Mvx.MvxAutoCompleteTextView
     android:id="@+id/actSignal"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_margin="5dp"
     android:completionThreshold="1"
     local:MvxItemTemplate="@layout/searchitemview"
     local:MvxBind="ItemsSource Items; PartialText ItemSearchTerm; SelectedObject Item;" />

我使用的ItemView:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="2dp"
    local:cardCornerRadius="2dp">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp">
        <TextView
            android:id="@+id/tvName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="20sp"
            local:MvxBind="Text Name" />
    </RelativeLayout>
</android.support.v7.widget.CardView>

视图模型:

public class FirstViewModel : MvxViewModel
{
    private ObservableCollection<ItemClass> _items;
    private ItemClass _item;
    private string _searchTerm;

    public ObservableCollection<ItemClass> Items
    {
        get => _items;
        set => SetProperty(ref _items, value);
    }

    public ItemClass Item
    {
        get => _item;
        set => SetProperty(ref _item, value);
    }

    public string ItemSearchTerm
    {
        get => _searchTerm;
        set => SetProperty(ref _searchTerm, value);
    }

    public FirstViewModel()
    {
        Init();
    }

    private void Init()
    {
        Items = new ObservableCollection<ItemClass>
        {
            new ItemClass
            {
                Name = "Test1",
            },
            new ItemClass
            {
                Name = "Test2",
            },
            new ItemClass
            {
                Name = "Test3",
            }
        };
    }
}

当我 运行 它并在其中输入一些内容时,我在输出中得到以下内容 window

[0:] mvx:Diagnostic: 15.18 Wait starting for T

这是我放置样本的 github:Github

I made a sample and cannot get it correctly

您应该在 ItemClass class 中实现 INotifyPropertyChanged 接口,完整代码如下:

public class ItemClass : INotifyPropertyChanged
{
    private string _name;
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
            this.RaisePropertyChanged("Name");
        }
    }

    private int _age;
    public int Age
    {
        get
        {
            return _age;
        }
        set
        {
            _age = value;
            this.RaisePropertyChanged("Age");
        }
    }

    public override string ToString()
    {
        return "Age is " + Age + ", Name is " + Name;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null))
        {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

I want to use a custom template with more info then only the name.

MvxAutoCompleteTextView 使用自定义模板:

 <MvxAutoCompleteTextView
      ...
      local:MvxItemTemplate="@layout/searchitemview"
      local:MvxBind="ItemsSource Items; PartialText CurrentTextHint; SelectedObject SelectedObject;" />

searchitemview.axml 中,您在 ItemClass class 中定义的绑定属性:

<LinearLayout
  ...
  <TextView
      ...
      local:MvxBind="Text Name" />
  <TextView
      ...
      local:MvxBind="Text Age" />
 </LinearLayout>

在您的 FirstViewModel class 中使用它:

public class FirstViewModel : MvxViewModel
{
    private static ObservableCollection<ItemClass> AutoCompleteList;

    public FirstViewModel()
    {
        AutoCompleteList = new ObservableCollection<ItemClass>()
         {
             new ItemClass
             {
                 Name = "Test1",
                 Age = 11
             },
             new ItemClass
             {
                 Name = "Test2",
                 Age = 12
             },
             new ItemClass
             {
                 Name = "Test3",
                 Age = 13
             }
        };
    }

    private ItemClass _SelectedObject;
    public ItemClass SelectedObject
    {
        get { return _SelectedObject; }
        private set
        {
            _SelectedObject = new ItemClass { Name = value.Name ,Age = value.Age};
            RaisePropertyChanged("SelectedObject");
        }
    }

    private List<ItemClass> _items = new List<ItemClass>();
    public List<ItemClass> Items
    {
        get
        {
            if (_items == null)
            {
                _items = new List<ItemClass>();
            }
            return _items;
        }
        set { _items = value; RaisePropertyChanged("Items"); }
    }

    private string _currentTextHint;
    public string CurrentTextHint
    {
        get
        { return _currentTextHint; }
        set
        {
            MvxTrace.Trace("Partial Text Value Sent {0}", value);
            //Setting _currentTextHint to null if an empty string gets passed here
            //is extremely important.
            if (value == "")
            {
                _currentTextHint = null;
                SetSuggestionsEmpty();
                return;
            }
            else
            {
                _currentTextHint = value;
            }

            if (_currentTextHint.Trim().Length < 2)
            {
                SetSuggestionsEmpty();
                return;
            }

            var list = AutoCompleteList.Where(i => (i.Name.ToString() ?? "").ToUpper().Contains(_currentTextHint.ToUpper()));
            if (list.Count() > 0)
            {
                Items = list.ToList();
            }
            else
            {
                SetSuggestionsEmpty();
            }
        }
    }

    private void SetSuggestionsEmpty()
    {
        Items = new List<ItemClass>();
    }
}

效果: