简体   繁体   中英

Xamarin.Forms how to transfer data from CollectionView to a different view?

I am working on a small project in xamarin, I have written a code to display my items in a collection view. It turned out great but after I click plus button to add it to my orders, I can't transfer the item to other view, I can't get the data by using get set, please help. here is my code:

xaml

<ContentPage.Content>
    <StackLayout>
        <Frame BackgroundColor="{AppThemeBinding Light=#F7F7F7, Dark=#191919}"
               Padding="0, 15" CornerRadius="0" HasShadow="False">
            <StackLayout Orientation="Horizontal" VerticalOptions="Center" HorizontalOptions="Center" Spacing="10">
                <Image Source="breakfast" HeightRequest="50"/>
                <Label Text="Breakfast" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"
                   TextColor="{AppThemeBinding Light=Black, Dark=White}" FontSize="30" FontAttributes="Bold"/>
            </StackLayout>
        </Frame>

        <StackLayout Padding="20" BackgroundColor="{AppThemeBinding Light=White, Dark=Black}" Spacing="50">

            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <CollectionView ItemsSource="{Binding Orders}">
                    <CollectionView.ItemsLayout>
                        <GridItemsLayout Orientation="Vertical" VerticalItemSpacing="30"/>
                    </CollectionView.ItemsLayout>
                    <CollectionView.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="100"/>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="50"/>
                                </Grid.ColumnDefinitions>

                                    <Image Source="{Binding ImageURL}"
                                           WidthRequest="50" HeightRequest="50"
                                           Grid.Column="0"/>
                                    <StackLayout Orientation="Vertical" Grid.Column="1">
                                        <Label x:Name="orderName" Text="{Binding OrderName}" FontSize="Title">
                                            <Label.GestureRecognizers>
                                                <TapGestureRecognizer Tapped="AddOrder" />
                                            </Label.GestureRecognizers>
                                        </Label>
                                        <Label Text="{Binding Price, StringFormat='{0}$'}" FontSize="Subtitle"/>
                                    </StackLayout>
                                    <ImageButton Grid.Column="2"
                                            Source="plus" HeightRequest="50" Clicked="AddOrder"/>

                            </Grid>
                        </DataTemplate>
                    </CollectionView.ItemTemplate>
                </CollectionView>
            </Grid>

            <StackLayout Padding="0, 120, 0, 0">
                <ImageButton Source="back" HorizontalOptions="Center"
                         HeightRequest="60" Clicked="Back"/>
            </StackLayout>
        </StackLayout>
    </StackLayout>
</ContentPage.Content>

this is the UI https://i.stack.imgur.com/QR133.png

After that I also tried assigning ImageButton with x:Name="addOrder" but I can't use it in.cs file,

cs file:

public partial class Breakfast : ContentPage
{
    public Breakfast()
    {
        InitializeComponent();
    }

    void Back(object sender, EventArgs args)
    {
        Application.Current.MainPage = new Main();
    }

    async void AddOrder(object sender, EventArgs args)
    {
        await this.DisplayToastAsync("added to orders", 3000);
    }
}

after pressing plus button it's running the AddOrder function because the DisplayToastAsync (or the popup window) is showing, there is no problem, the problem is how can I move the list item to My Orders View?

All answers and comments are appreciated!

you can do something like this

async void AddOrder(object sender, EventArgs args)
{
    var btn = (ImageButton)sender;
    var item = (MyClassName)btn.BindingContext;

    // navigate to the next page and pass item on the constructor
    Navigation.PushAsync(new MyNextPage(item));
}

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