简体   繁体   中英

Xamarin forms CollectionView passing data

I need some help, I am doing a Pluralsight course on Xamarin, and I cannot pass the data from my CollectionView to my DetailPage.

The way that I have it set up, is that my Contructor for my DetailPage receives a "Pie" as an argument and in the page, where I called currentSelection and pass the data, for some weird reason is null.

      private async void PiesCollection_SelectionChanged(object sender, SelectionChangedEventArgs e) {

            Pie selectedItem = e.CurrentSelection as Pie;
            await Navigation.PushAsync(new DetailPage(selectedItem));
        }
    }
    public DetailPage(Pie p) {
            InitializeComponent();
            getData(p);
        }

        private void getData(Pie pie) {
            img_pie.Source = pie.ImageUrl;
            lbl_pieName.Text = pie.PieName;
            lbl_price.Text = pie.Price.ToString("C");
            lbl_stock.Text = pie.InStock;
            lbl_description.Text = pie.Description;
        }

When I put a breakpoint, I can see that "CurrentSelection" has my data, but my SelectedItem, witch I am passing to a other page, gets null 在此处输入图像描述

I am really sorry if this is a stupid question

CollectionView allows you to select multiple items, so CurrentSelection contains a list of values

private async void PiesCollection_SelectionChanged(object sender, SelectionChangedEventArgs e) {

   if (e.CurrentSelection != null && e.CurrentSelection.Count > 0)
     Pie selectedItem = e.CurrentSelection[0] as Pie;
     await Navigation.PushAsync(new DetailPage(selectedItem));
   }
}

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