在 Xamarin.CarouselView 中不显示图像 [英] Not showing image in Xamarin.CarouselView
本文介绍了在 Xamarin.CarouselView 中不显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 xamarin 构建一个应用程序,该应用程序从网络服务获取图像并绑定到轮播视图.
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.
这是我的 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>
这是我的代码
ImageSources = await Task.Run(() =>
{
var productViewData = new ProductViewData();
return productViewData.GetProductImages(Product.id.GetValueOrDefault());
});
carouselViewImage.ItemsSource = ImageSources;
I 绑定的基础是 ImageSource
The base I binding to is ImageSource
更新:
我尝试另一种创建新类的方法:
I try another way which create new class:
class ImageTest
{
public ImageSource Image { get; set; }
}
并使用 foreach 创建新的 ImageSource 列表
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;
并更改xaml
<Image Source="{Binding Image}" VerticalOptions="Center" HeightRequest="180" WidthRequest="100"/>
它只是工作.但我不想要这个=(
And it just work. But i don't want this=(
感谢您的帮助.
推荐答案
根据您的评论 ItemsSource
是 List
.在这种情况下,绑定是错误的,应该这样做:
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}"/>
这篇关于在 Xamarin.CarouselView 中不显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文