在xamarin表单4.4.0中将图像添加到轮播视图

时间:2020-03-10 19:18:29

标签: c# xamarin xamarin.forms carousel

我有用于创建旋转木马视图的代码

    <CarouselView x:Name="TheCarousel">
        <CarouselView.ItemsSource>
            <x:Array Type="{x:Type x:String}">
                <x:String>Hey...</x:String>
                <x:String>Psssst...!</x:String>
                <x:String>Did you check out...</x:String>
                <x:String>The awesome new CarouselView</x:String>
                <x:String>In Xamarin.Forms 4.4?!</x:String>
                <x:String>? ?</x:String>
            </x:Array>
        </CarouselView.ItemsSource>
        <CarouselView.ItemTemplate>
            <DataTemplate>
                <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
                    <Label Text="{Binding .}" HorizontalTextAlignment="Center" FontSize="Title" />
                </StackLayout>
            </DataTemplate>
        </CarouselView.ItemTemplate>
    </CarouselView>`

我该如何使用图像而不是字符串来查看图像? 我在Visual Studio 2019上使用xamarin 4.3.0

1 个答案:

答案 0 :(得分:1)

在模板中使用Image代替Label

<CarouselView x:Name="TheCarousel">
    <CarouselView.ItemsSource>
        <x:Array Type="{x:Type x:String}">
            <x:String>[image url goes here]</x:String>
            <x:String>[image url goes here]</x:String>
            <x:String>[image url goes here]</x:String>
        </x:Array>
    </CarouselView.ItemsSource>
    <CarouselView.ItemTemplate>
        <DataTemplate>
            <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
                <Image Source="{Binding .}" />
            </StackLayout>
        </DataTemplate>
    </CarouselView.ItemTemplate>
</CarouselView>`