TheSearcher
TheSearcher

Reputation: 64

Passing data from list to new view xamarin

Ive created a picker with data inside, when selected the data is added to a list. I want to the values that are added to the list to follow to the next page when calculate button is pressed.. tried a few different approaches but i cant get it right..

.cs

// add from picker to listview function

        ObservableCollection<LayersClass> listProducts = new ObservableCollection<LayersClass>();

        private void MainPicker_SelectedIndexChanged(object sender, EventArgs e)
        {
            // feedback popup box
             var product = MainPicker.Items[MainPicker.SelectedIndex];
             DisplayAlert(product, "Layer added to calculation list", "OK");
            // if selected add to list          
            if (null != product)
            {
                LayersClass layer = new LayersClass();
                layer.Product = product;
                listProducts.Add(layer);
            }        
        }

        //calculate button
        private async void Button_Clicked(object sender, EventArgs e)
        {

// send selected values to CalculationPage ??

            await Navigation.PushAsync(new CalculationPage());

        }

xaml:

<ListView  
                  x:Name="productsListView"
                  HasUnevenRows="False" 
                  HorizontalOptions="FillAndExpand"
                  VerticalOptions="FillAndExpand" 
                  BackgroundColor="White">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.ContextActions>
                            <MenuItem Clicked="MenuItem_Clicked" Text="Delete" IsDestructive="true" CommandParameter="{Binding .}" />
                        </ViewCell.ContextActions>
                        <StackLayout>
                            <Label Text="{Binding Product}"></Label>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <Button Margin="0,0,0,90" Text="Calculate" Clicked="Button_Clicked"  FontSize="Medium" TextColor="#00AB8E"  HorizontalOptions="Center" BackgroundColor="Transparent"/>

Upvotes: 0

Views: 50

Answers (1)

İsmail &#199;apkın
İsmail &#199;apkın

Reputation: 36

You should use parameter to pass data to the another page.

private async void Button_Clicked(object sender, EventArgs e) {

       // send selected values to CalculationPage ??
        //Also you should write there an event.

         var item =  sender as Button;
         var selectedItem = item as LayersClass;
        await Navigation.PushAsync(new CalculationPage(item));

    }

public CalculationPage(string item){ //when you type here dot after typing item like this -> item. you can see the layerclass items. }

Upvotes: 1

Related Questions