通过 mvvm 模式使用列表视图上搜索栏的搜索命令

问题描述 投票:0回答:1

我是 xamarin mvvm patter 的初学者。目前,我正在尝试创建一个搜索栏,从名称列表中搜索单词。我尝试在视图模型上的comman函数上编写一些代码,并将其绑定到视图上搜索栏的

SearchCommand
上。但这没有用。这是我的代码

namespace HelloWorld.ViewModel
{

    public class CustViewModel : INotifyPropertyChanged
    {

        private custmodel _custmodel;


        public custmodel custmodel
        {
            get { return _custmodel; }
            set
            {
                _custmodel = value;
                NotifyPropertyChanged();
            }
        }

        private string _message;
        public string message
        {
            get { return _message; }
            set
            {
                _message = value;
                NotifyPropertyChanged();
            }
        }

        private ObservableCollection<string> _list;
        public ObservableCollection<string> Items
        {
            get
            {
        return _list;
            }
            set
            {
                _list = value;
                NotifyPropertyChanged();
            }
        }

        public Command SaveCommand
        {
            get
            {
                return new Command(() =>
                {
                    message = "Your task : " + custmodel.name + ", " + custmodel.surname + " was successfully saved!";
                });
            }
        }

        private string _bar;

        public string Bar
        {
            get { return _bar; }
            set { _bar = value;
        }
    }

    public Command SearchCommand
{
    get
    {
            return new Command(() =>
            {
                string keyword = _bar;
                IEnumerable<String> searchresult = _list.Where(name => name.Contains(keyword));
                _list = new ObservableCollection<string>(searchresult);
                NotifyPropertyChanged();
            }
                );


    }
}

public CustViewModel()
{

    custmodel = new custmodel
    {
        name = "Aasish",
        surname = "Gurung",
        email = "[email protected]"
    };
    _list = new ObservableCollection<string>();
    _list.Add("Saurab");
    _list.Add("Basanta");
    _list.Add("Abhishek");
    _list.Add("Surace");
    _list.Add("Amir");

    }

    public event PropertyChangedEventHandler PropertyChanged;


    //public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

    }
}

这是我的 xaml 文件

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="HelloWorld.Styling"
         BackgroundColor="AntiqueWhite" Title="Hello"
         xmlns:converters="clr-namespace:HelloWorld.Converters; assembly=HelloWorld">

<StackLayout>
    <SearchBar x:Name="MainSearchBar" Text="{Binding Bar}"
               SearchCommand="{Binding SearchCommand}"/>
    <ListView ItemsSource="{Binding Items}"/>
</StackLayout>

xamarin mvvm data-binding xamarin.forms
1个回答
2
投票

首先,确保您将

ContentPage
BindingContext
设置为您的
CustViewModel

此外,您应该停止向

_list
分配和添加内容,而是向您的公共
Items
属性分配和添加内容。
Items
是在被分配后将触发
NotifyPropertyChanged()
方法的方法。

因此将您的

SearchCommand
更改为:

return new Command(() => {
    string keyword = _bar;
    IEnumerable<String> searchresult = _list.Where(name => name.Contains(keyword));

    Items = new ObservableCollection<string>(searchresult);

    //NotifyPropertyChanged(); //There is no reason to trigger NotifyPropertyChanged on this command each time the getter is run, I would imagine that this could cause an infinite loop
});
© www.soinside.com 2019 - 2024. All rights reserved.