问题
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