简体   繁体   English

将图像添加到列表视图中的轮播 xamarin forms

[英]Add images to carousel within a listview xamarin forms

Firts let me explain what I want to do.首先让我解释一下我想做什么。 I have a listview which shows some products.我有一个显示一些产品的列表视图。 When I click on a item, a window pops up with the details of that product.当我点击一个项目时,一个 window 会弹出该产品的详细信息。 Now, in those details I want to show more pictures of that product with a carousel slide function and below those images I want to show the details and other stuff.现在,在这些细节中,我想展示更多带有旋转木马幻灯片 function 的产品图片,在这些图片下方我想展示细节和其他内容。 This is what I made so far but the images won't show.这是我到目前为止所做的,但图像不会显示。

<ContentPage.Content>
        <StackLayout Padding="5,5,5,5">
            <ListView ItemsSource="{Binding productDetail}" HasUnevenRows="True"
                      x:Name="lstProductDetail">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Grid x:Name="productDetailsGrid"
                          Padding="5, 5, 5, 5">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="auto"></RowDefinition>
                                    <RowDefinition Height="auto"></RowDefinition>
                                    <RowDefinition Height="auto"></RowDefinition>
                                    <RowDefinition Height="auto"></RowDefinition>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition></ColumnDefinition>
                                </Grid.ColumnDefinitions>
                                <Label Text="{Binding productHeader}" Grid.Column="0" Grid.Row="0"
                                        TextColor="#FFFFFF"
                                        FontSize="25"
                                       BackgroundColor="Blue"
                                        FontAttributes="Bold"></Label>
                                <CarouselView Grid.Column="0" Grid.Row="1">
                                    <CarouselView.ItemTemplate>
                                        <DataTemplate>
                                            <StackLayout HorizontalOptions="Center">
                                                <Image Source="{Binding productImage}"/>
                                            </StackLayout>
                                        </DataTemplate>
                                    </CarouselView.ItemTemplate>
                                </CarouselView>
                                <Label Text="{Binding productPrice}" 
                                        FontSize="24"
                                        FontAttributes="Bold"
                                        TextColor="Red"
                                        Grid.Column="0"
                                        Grid.Row="2"/>
                                <Label Text="{Binding productDescription}" 
                                   FontSize="Medium" 
                                   TextColor="#000000"
                                   Grid.Column="0"
                                   Grid.Row="3"/>
                            </Grid>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>

And in my.CSfile:在我的.CSfile 中:

public IList<productDetails> productDetail { get; set; }
        private CultureInfo nfi = new CultureInfo("nl-NL", false);
        private int product_id;

        public Item Item { get; set; }
        public int productID {
            get { return product_id; }
            set { product_id = value; } 
        }
        public NewItemPage(int productId, string itemName)
        {
            InitializeComponent();
            productDetail = new List<productDetails>();
            productDetail.Add(new productDetails { productHeader = "Test ding", productDescription = "Desc", productImage = "https://www.electronicabalie.nl/105297-large_default/philips-ce600n-2-din-autoradio-met.jpg", productManufacturer = "Manufacturer", productPrice = "129,95" });
            
            //loadProductDetail(productId);
            /*product_id = productId;
            Item = new Item
            {
                Text = itemName + " " + product_id.ToString(),
                Description = "This is description.",
                Id = product_id.ToString()
            };*/
            BindingContext = this;
        }

Public class part:公共 class 部分:

public class productDetails
        {
            public string productHeader { get; set; }
            public string productImage { get; set; }
            public string productManufacturer { get; set; }
            public string productPrice { get; set; }
            public string productDescription { get; set; }
        }

I think I need to add some extra array to add the images but somehow the carousel doesn't get the Binding for the images.我想我需要添加一些额外的数组来添加图像,但不知何故轮播没有获得图像的绑定。 Someone knows what I'm doing wrong?有人知道我在做什么错吗? Thanks!谢谢!

You didn't set the ItemsSource of CarouselView .您没有设置CarouselViewItemsSource

in xaml在 xaml

<CarouselView Grid.Column="0" Grid.Row="1" ItemsSource="{Binding ImageList}">
       <CarouselView.ItemTemplate>
            <DataTemplate>
                 <StackLayout HorizontalOptions="Center">
                      <Image Source="{Binding .}"/>
                 </StackLayout>
             </DataTemplate>
       </CarouselView.ItemTemplate>
</CarouselView>

in model在 model

public class productDetails
{
    public string productHeader { get; set; }
    public IList<string> ImageList { get; set; }
    public string productManufacturer { get; set; }
    public string productPrice { get; set; }
    public string productDescription { get; set; }
}

in ViewModel在视图模型中

 public ObservableCollection<productDetails> productDetail { get; set; } 
public ViewModel()
        {

            productDetail = new ObservableCollection<productDetails>();

            for(int i = 0;i<5;i++)
            {
                var item = new productDetails()
                {
                    productHeader = "header",
                    productPrice = "$200.00",
                    productDescription = "Product Details",
                    productManufacturer = "xxx",
                    ImageList = new List<string>() {"test.png","test.png", "test.png" },

                };

                productDetail.Add(item);
            }          
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM