Xamarin形式:如何使每个轮播页面具有不同的颜色

时间:2019-10-25 04:04:43

标签: forms xamarin carousel

我创建了包含幻灯片集合的Carousel视图。每张幻灯片都包含图像,消息和颜色。它存储在ObservableCollection中。我的收藏中有三种颜色。第一张幻灯片/页面应为黄色,第二张应为红色,第三张应为蓝色。我的问题是,当应用启动时,所有的幻灯片在轮播中都是蓝色的。我需要每个幻灯片/页面具有不同的颜色。

 Carousel.ItemsSource =   slides = new ObservableCollection<Slides>(new[]
            {
                new Slides("money", "Some Description", BackgroundColor = Color.Yellow),
                new Slides("money", "Some Description2", BackgroundColor = Color.Red),
                new Slides("money", "Some Description3",BackgroundColor = Color.Blue)});



<Control:CarouselViewControl x:Name="Carousel"
                             ShowIndicators="True"
                             BackgroundColor="{Binding Color}"
                             CurrentPageIndicatorTintColor="Black">
   <Control:CarouselViewControl.ItemTemplate>
       <DataTemplate>
      <Grid>
          <Grid.RowDefinitions>
              <RowDefinition Height="*"/>
              <RowDefinition Height="*"/>
          </Grid.RowDefinitions>
          <ContentView Grid.Row="0" Padding="60,30,60,0">
              <Image Source="{Binding Image}" Aspect="AspectFit"/>
          </ContentView>
          <ContentView Grid.Row="1" Padding="20,50,20,0">
              <Label Text="{Binding Message}" TextColor="black"
                     HorizontalOptions="CenterAndExpand"
                     FontSize="Large"/>

          </ContentView>

      </Grid>

       </DataTemplate>
   </Control:CarouselViewControl.ItemTemplate>
</Control:CarouselViewControl>

我希望每个页面都有不同的颜色。

1 个答案:

答案 0 :(得分:0)

您正在将BackgroundColor绑定到CarouselViewControl,这将为整个视图(而不是不同的幻灯片)设置它。

此外,存储在ItemsSource中的项目不代表CarouselViewControl中的任何视图,而是数据对象。仅仅因为ItemsSource集合中的对象具有值BackgroundColorBackgroundColor中相应视图的CarouselViewControl不会自动设置。

要设置页面的背景,您必须在DataTemplate内部进行操作,并将BackgroundColor的{​​{1}}属性绑定到Grid的相应属性

Slide
相关问题