我想根据搜索从我的视频类返回列表视图条目。在Videos类中,有单独的Video对象具有多个属性,如标题,视图,作者和上传日期。
如何根据搜索栏查询视频以返回视频对象? 最后,我希望它根据title属性的查询返回视频对象的所有属性。
如果title.contatins(keyword): 返回视频对象
Homepage.xaml.cs:
public partial class Homepage : ContentPage
{
List<Video> Videos = new List<Video>
{
new Video
{
Title = "Video 1",
Author = "FedEx",
Views = 322,
Upload_DateTime = new DateTime(1996, 8, 26),
ImageURL = "icon.png",
},
new Video
{
Title = "Video 2",
Author = "FedEx",
Views = 554,
Upload_DateTime = new DateTime(2017, 1, 23),
ImageURL = "icon.png",
},
new Video
{
Title = "Video 3",
Author = "FedEx",
Views = 23,
Upload_DateTime = new DateTime(2012, 3, 11),
ImageURL = "icon.png",
},
};
public Homepage()
{
InitializeComponent();
VideoListView.ItemsSource = Videos;
}
private void MainSearchBar_Pressed(object sender, EventArgs e)
{
var keyword = MainSearchBar.Text;
VideoListView.ItemsSource = Videos.Where(Title = keyword);
}
}
Video.cs:
class Video
{
public string Title { get; set; }
public string Author { get; set; }
public int Views { get; set; }
public DateTime Upload_DateTime { get; set; }
public string ImageURL { get; set; }
}
注意:MainSearchBar_Pressed事件处理程序让我失望
答案 0 :(得分:1)
解决方案:
private void MainSearchBar_Pressed(object sender, EventArgs e)
{
var keyword = MainSearchBar.Text;
VideoListView.ItemsSource = Videos.Where(a => a.Title.Contains(keyword));
}