Send an object to a ListView in Xamarin and return a property of the object

  Kiến thức lập trình

My program is a search bar that lists artists’s names using ListView in Xamarin. Now I want to update my program so that when an artist’s name is clicked it goes to another page where I am displaying detailed information about the artist. So now instead of a List<string>, I want to send a List<Artist>(Containing the properties string ArtistName and string ArtistID) to the page, using ListView. I want to display the string ArtistName int the ListView and when that artist is clicked, I want to send his string ArtistID to the method SuggestionListView_OnItemSelected, where I can handle it further.
This is my MainPage.xaml.cs:

public partial class MainPage : ContentPage
    private readonly IArtistsSearchService artistsSearchService;

    public MainPage(MainViewModel mainViewModel, IArtistsSearchService artistsSearchService)
        BindingContext = mainViewModel ?? throw new ArgumentNullException(nameof(mainViewModel));
        this.artistsSearchService = artistsSearchService;

    private async void ArtistsSearchBar_OnTextChanged(object sender, TextChangedEventArgs e)
        string keyword = ArtistsSearchBar.Text?.ToLower() ?? string.Empty;
        List<Artist> result = await artistsSearchService.GetArtistsAsync(keyword);
        SuggestionListView.ItemsSource = result; //The code used to send List<string>, now i want to send the List<Artist>

    private void SuggestionListView_OnItemSelected(object sender, SelectedItemChangedEventArgs e)
        string artistID = string.Empty; //I need the ID of the artist that is selected from the listview

And here is my MainPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns=""


        <StackLayout Orientation="Vertical">
            <SearchBar x:Name="ArtistsSearchBar" 
               Placeholder="Search for an artist..." 
               TextChanged="ArtistsSearchBar_OnTextChanged" />
            <ListView x:Name="SuggestionListView" /> 



This is my first Xamarin app, so don’t go hard on me. I can provide more details about the code if needed.

private void SuggestionListView_OnItemSelected(object sender, SelectedItemChangedEventArgs e)
    var artist = (Artist)e.Item;

    // now you can get the ID or any other property from the artist object

and you need to add an ItemTemplate

        <TextCell Text="{Binding ArtistName}" />