简体   繁体   中英

How to get ListView's ViewCell data and send it to another page in Xamarin.Forms

This is my second project since I started Xamarin.Forms. Basically what I'm trying to do is list all the contacts in the users mobile and once the user selects a contact from the list I want to send the user to another Page to do some other actions.

At the moment I'm successful in getting all the contacts into a ListView, also I know how to route the user to another page. But the problem is I don't know how to get the data inside the ViewCell when tapped on it.

At the moment this is the code I'm using,

MainPage.xaml

<ContentPage.Content>
      <StackLayout>
            <SearchBar x:Name="filterText"
                        HeightRequest="40"
                        Text="{Binding SearchText}" />
            <ListView x:Name="lstvv" ItemSelected="lstvv_ItemSelected" ItemsSource="{Binding FilteredContacts}"
                        HasUnevenRows="True">
                <ListView.ItemTemplate>
                    <DataTemplate>
                      <ViewCell>
                            <StackLayout Padding="10"
                                         Orientation="Horizontal">
                                <Image  Source="{Binding Image}"
                                        VerticalOptions="Center"
                                        x:Name="image"
                                        Aspect="AspectFit"
                                        HeightRequest="60"/>
                              <StackLayout VerticalOptions="Center">
                                    <Label x:Name="lblname" Text="{Binding Name}"
                                       FontAttributes="Bold">
                                    </Label>
                                    <Label Text="{Binding PhoneNumbers[0]}"/>
                                 <Label Text="{Binding Emails[0]}"/>
                            </StackLayout>
                            </StackLayout>
                        </ViewCell>
                     </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>

MainPage.xaml.cs

public partial class MainPage : ContentPage
    {
        public MainPage(IContactsService contactService)
        {
            BindingContext = new MainViewModel(contactService);
            InitializeComponent();
        }

        private async void lstvv_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            await DisplayAlert("Alert", e.SelectedItem.ToString(), "Ok");
        }
    }

And this is the DisplayAlert

how to get the data inside the ViewCell

this is literally what e.SelectedItem is - it is the selected Contact , you just need to cast it

private async void lstvv_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
    var contact = (Contact)e.SelectedItem;
    await DisplayAlert("Alert", contact.Name, "Ok");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM