将数据从列表传递到新视图xamarin

时间:2018-12-07 09:20:54

标签: xamarin

我已经创建了一个内部带有数据的选择器,选择后会将数据添加到列表中。我想添加到列表中的值,然后在按下“计算”按钮时转到下一页。尝试了几种不同的方法,但我无法正确处理。

.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"/>

1 个答案:

答案 0 :(得分:1)

您应该使用参数将数据传递到另一页。

私有异步void Button_Clicked(对象发送者,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(字符串项目){ //当您在此处键入类似-> item的项目后,在此处键入点号。您可以看到layerclass项目。 }