Khoa
Khoa

Reputation: 263

How To Display Product Details When Selecting A Product using Xamarin Forms

I'm currently building a mobile app using Xamarin Forms and it's my first time doing so.

It's basically an ecommerce shopping cart where I pull the data using REST API, it shows the list of products and then the user can add to cart.

I need help in the best way to display the individual product details when a user clicks on the product within the list.

So for example, when I click on the product category, it will show a list of products.

How do I display the individual product details on another page when I select a product?

Any help would be greatly appreciated!

Here's my productpage.xaml where it displays all the products correctly.

<ListView x:Name="productsListView"
                      HasUnevenRows="True"                       
                        VerticalOptions="FillAndExpand"
                      SeparatorVisibility="None"
                      ItemSelected="OnItemSelected">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <ViewCell.View>
                                <Frame HasShadow="True" Padding="20" Margin="20">
                                    <StackLayout>
                                        <Image Source="{Binding featured_src}"/>
                                        <Label Text="{Binding title}" FontSize="Medium"/>
                                        <Frame BackgroundColor="Red" Padding="5" HorizontalOptions="Center" WidthRequest="80" HeightRequest="20" CornerRadius="00">
                                            <Label WidthRequest="40" Text="{Binding price , StringFormat='${0}'}" TextColor="White" HorizontalTextAlignment="Center"></Label>
                                        </Frame>

                                    </StackLayout>
                                </Frame>
                        
                            </ViewCell.View>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

I just need to figure out how to display the individual products.

Upvotes: 1

Views: 1011

Answers (1)

nevermore
nevermore

Reputation: 15806

How do I display the individual product details on another page when I select a product?

First, your app should start with a NavigationPage:

public App()
{
    InitializeComponent();

    MainPage = new NavigationPage(new MainPage());
}

You can push to another page with specific product dataModel to show the selected product details in OnItemSelected method:

private void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
{

    dataModel selectedProductModel = e.SelectedItem as dataModel;

    Navigation.PushAsync(new ProductDetail(selectedProductModel));
}

And you should have a ProductDetail page to show the details:

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

    public ProductDetail(dataModel model)
    {
        InitializeComponent();

        //get the produce info from model and show it in this page
    }
}

Upvotes: 2

Related Questions