Not showing image in Xamarin.CarouselView

拥有回忆 提交于 2021-02-11 15:23:27

问题


I'm using xamarin build an app which get image from webservice and bind to carousel view.

But I can't see the image showing.

This is my xaml.

<StackLayout HeightRequest="250">
                <cv:CarouselView x:Name="carouselViewImage">
                    <cv:CarouselView.ItemTemplate>
                        <DataTemplate>
                            <Image Source="{Binding base}" 
                                   VerticalOptions="Center"
                                   HeightRequest="180" WidthRequest="100"/>
                        </DataTemplate>
                    </cv:CarouselView.ItemTemplate>
                </cv:CarouselView>
            </StackLayout>

This is my code

ImageSources = await Task.Run(() =>
            {
                var productViewData = new ProductViewData();
                return productViewData.GetProductImages(Product.id.GetValueOrDefault());
            });

            carouselViewImage.ItemsSource = ImageSources;

The base I binding to is ImageSource

Updated:

I try another way which create new class:

class ImageTest
{
    public ImageSource Image { get; set; }
}

And using foreach to create new list of ImageSource

ImageSources = await Task.Run(() =>
            {
                var productViewData = new ProductViewData();
                return productViewData.GetProductImages(Product.id.GetValueOrDefault());
            });
            var testImage = new List<ImageTest>();
            //Add this foreach 
            foreach (var imageSource in ImageSources)
            {
                testImage.Add(new ImageTest { Image = imageSource });
            }


            carouselViewImage.ItemsSource = testImage;

And change xaml

 <Image Source="{Binding Image}" VerticalOptions="Center" HeightRequest="180" WidthRequest="100"/>

And it just work. But i don't want this=(

Thanks for any helps.


回答1:


According to your comment ItemsSource is List<ImageSource>. In that case the binding is wrong, this is how it should be done:

<Image Source="{Binding}"/>


来源:https://stackoverflow.com/questions/54843494/not-showing-image-in-xamarin-carouselview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!