我有轮播视图,我想使用XAML获取轮播中下一个对象的属性颜色。我的XAML:
<CarouselView
HeightRequest="300"
PeekAreaInsets="100"
IndicatorView="indicatorView"
ItemsSource="{Binding TrafficColors}">
<CarouselView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Frame HasShadow="False"
CornerRadius="5"
Padding="0"
Margin="10, 0"
WidthRequest="200"
HeightRequest="300"
>
<Frame.Background>
<LinearGradientBrush EndPoint="1,1">
<GradientStop Color="{Binding HEX, StringFormat='#{0}'}" Offset="0.4" />
<GradientStop Color="#CFDEF3" Offset="0.77" />
</LinearGradientBrush>
</Frame.Background>
<!--
<Frame.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding Source={RelativeSource AncestorType={x:Type view:EDocsMainPageViewModel}}, Path=GoToEDoc}"
CommandParameter="{Binding Id}"
/>
</Frame.GestureRecognizers>
-->
<StackLayout>
<Label Text="{Binding Name}"
TextColor="Black"
FontSize="22"
Padding="10"
HorizontalOptions="Center"
VerticalOptions="Center" />
<Label Padding="10" Text="{Binding ShiftCode, StringFormat='Номер МЛ:{0}'}" TextColor="Black" FontSize="14" />
<Label Padding="10" Text="{Binding RegDate, StringFormat='Дата:{0:yyyy.MM.dd HH:mm}'}" TextColor="Black" FontSize="14" />
</StackLayout>
</Frame>
</StackLayout>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
例如,我想得到集合中下一个Frame
的BackgroundColor
。例如,在HTML
中,我有对象ID或类名,通过这个键和JS我可以得到一些东西。那么XAML呢?我可以为集合中的对象设置键,并通过这个键以某种方式从XAML中得到值吗?我将感谢任何信息。
另外,我不想在视图模型中再添加一个属性,我想在XAML中添加。
1条答案
按热度按时间hpxqektj1#
例如,我想获取集合中下一个Frame的BackgroundColor。例如,在HTML中,我有对象ID或类名,通过这些键和JS,我可以得到一些东西。那么XAML呢?我可以为集合中的对象设置键,然后通过这个键以某种方式从XAML中获取值吗?
我们无法得到集合中下一个
Frame
的BackgroundColor
,因为我们不知道将选择哪一项。但是我们可以得到currentItem
和previousItem
。我们知道
CarouselView
的项使用CarouselView.ItemTemplate
的相同的DataTemplate
,所以我们不能通过DataTemplate
布局中视图的x:Name
得到下一个Frame
的BackgroundColor
。既然你把
bind
的颜色传给属性Frame.Background
,那么你就可以通过事件OnCurrentItemChanged
得到currentItem
和previousItem
。因此,我建议在
ItemsSource
(TrafficColors
)的Item中添加一个属性(例如BackgroundColor
)。然后,您将在事件
OnCurrentItemChanged
中获得currentItem
和previousItem
:例如:
TestPage.xaml
的有关
CarouselView
的详细信息,请查看:Configure CarouselView interaction。关于如何绑定颜色,可以查看线程:https://stackoverflow.com/questions/72901517/net-maui-how-to-reference-a-color-in-a-binding。