forms - Xamarin Forms:如何使每个轮播页面具有不同的颜色
问题描述
我创建了包含幻灯片集合的轮播视图。每张幻灯片都包含图像、消息和颜色。它存储在 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>
我希望每一页都有不同的颜色。
解决方案
您正在绑定BackgroundColor
for CarouselViewControl
,这将为整个视图设置它,而不是为不同的幻灯片设置它。
此外,存储在 中的项目ItemsSource
不代表 中的任何视图CarouselViewControl
,而是代表数据对象。仅仅因为ItemsSource
集合中的一个对象有一个值BackgroundColor
,BackgroundColor
相应视图中的CarouselViewControl
不是自动设置的。
要设置页面的背景,您必须在 中操作并将 的属性DataTemplate
绑定到 的相应属性。BackgroundColor
Grid
Slide
<DataTemplate>
<Grid BackgroundColor="{Binding BackgroundColor}"> <!-- Bind the grids BackgroundColor to the one of the Slide -->
<!-- the grids content -->
</Grid>
</DataTemplate>
推荐阅读
- angular - 对象对象不是 PostCSS 插件 - 构建 nrwl 库项目时出错
- java - 在 Netbeans 上的 Glassfish 处理 derby 数据库时遇到 ClassNotFoundException
- jbpm - 如何将本地项目导入 JBPM 业务中心而不是使用 GIT
- fortran - 为什么 MPI_REDUCE 在编译时返回语法错误?
- php - 如何使用while循环php解决这个问题
- nestjs - ExceptionHandler 在nest.js 中找不到“配置文件”的元数据
- shell - 列出子目录并在 shell 脚本中使用 select 和 case
- android - 如何读取包含空格的文件路径?
- excel - Excel问题,有点像复利,但不是
- python - 将 JSON 文件拆分为两个不同的 XML 文件