Connor Meeks
Connor Meeks

Reputation: 501

Xamarin Custom List View Search

I want to return a list view entry from my Videos Class based on a search. Inside the Videos class, there are individual Video objects with multiple attributes such as title, views, author, and upload date.

How would I query the Videos to return Video objects based on a search bar? In the end, I want it to return all the attributes of the video object based on a query of the title attribute.

If title.contatins(keyword): return the video object

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; }
}

NOTE: The MainSearchBar_Pressed event handler is what is throwing me off

Upvotes: 0

Views: 151

Answers (1)

Connor Meeks
Connor Meeks

Reputation: 501

Solution:

        private void MainSearchBar_Pressed(object sender, EventArgs e)
    {
        var keyword = MainSearchBar.Text;
        VideoListView.ItemsSource = Videos.Where(a => a.Title.Contains(keyword));

    }

Upvotes: 1

Related Questions