Xamarin 使用mvvm模式的列表视图上搜索栏的搜索命令
我是xamarin mvvm模式的初学者。目前,我正在尝试创建一个搜索栏,从名称列表中搜索单词。我试图在视图模型上的comman函数上编写一些代码,并将其绑定到视图上搜索栏的Xamarin 使用mvvm模式的列表视图上搜索栏的搜索命令,xamarin,mvvm,data-binding,xamarin.forms,Xamarin,Mvvm,Data Binding,Xamarin.forms,我是xamarin mvvm模式的初学者。目前,我正在尝试创建一个搜索栏,从名称列表中搜索单词。我试图在视图模型上的comman函数上编写一些代码,并将其绑定到视图上搜索栏的SearchCommand。但它不起作用。这是我的密码 namespace HelloWorld.ViewModel { public class CustViewModel : INotifyPropertyChanged { private custmodel _custmodel;
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 = "iamaaceez@yahoo.com"
};
_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));
}
}
}
名称空间HelloWorld.ViewModel
{
公共类CustViewModel:INotifyPropertyChanged
{
私人客户模式(custmodel);;
公共保管模式
{
获取{return\u custmodel;}
设置
{
_模型=价值;
NotifyPropertyChanged();
}
}
私有字符串消息;
公共字符串消息
{
获取{return\u message;}
设置
{
_消息=值;
NotifyPropertyChanged();
}
}
私有可观察收集列表;
公共可观测收集项目
{
得到
{
返回列表;
}
设置
{
_列表=值;
NotifyPropertyChanged();
}
}
公共命令SaveCommand
{
得到
{
返回新命令(()=>
{
message=“您的任务:“+custmodel.name+”,“+custmodel.name+”已成功保存!”;
});
}
}
私人字符串(u bar),;
公共字符串栏
{
获取{return\u bar;}
设置{u bar=value;
}
}
公共命令搜索命令
{
得到
{
返回新命令(()=>
{
字符串关键字=_栏;
IEnumerable searchresult=_list.Where(name=>name.Contains(关键字));
_列表=新的ObservableCollection(搜索结果);
NotifyPropertyChanged();
}
);
}
}
公共CustViewModel()
{
custmodel=新custmodel
{
name=“aassish”,
姓氏=“古隆”,
电子邮件=”iamaaceez@yahoo.com"
};
_列表=新的ObservableCollection();
_列表。添加(“Saurab”);
_列表。添加(“巴桑塔”);
_添加(“阿披舍克”);
_列表。添加(“表面”);
_列表。添加(“Amir”);
}
公共事件属性更改事件处理程序属性更改;
//公共事件属性更改事件处理程序属性更改;
私有void NotifyPropertyChanged([CallerMemberName]字符串propertyName=”“)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的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>
首先,确保将
ContentPage
的BindingContext
设置为CustViewModel
此外,您应该停止向\u list
分配和添加内容,而是将内容分配和添加到您的公共项目
属性。项目
是将在分配给时触发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 image that this could cause an infinite loop
});
返回新命令(()=>{
字符串关键字=_栏;
IEnumerable searchresult=_list.Where(name=>name.Contains(关键字));
Items=新的ObservableCollection(搜索结果);
//NotifyPropertyChanged();//没有理由每次运行getter时都在这个命令上触发NotifyPropertyChanged,我认为这可能会导致无限循环
});
谢谢,感谢您的帮助。如果搜索栏上的文本为空,我想返回包含完整列表的列表视图。我该怎么做?@AasishGrg最好是问一个新问题,而不是在同一篇文章中问多个问题,但您必须保留一个包含所有项目的单独列表,或者对您的数据API进行新的调用(如果有)这是一张完整的单子。