如何FindByName
在CollectionView
withGestureRecognizers
作为调用中实现。如有错误请指正。
private void ItemTapped(object sender, EventArgs e)
{
SelectedAmount = (int)0.0f;
var grid = sender as Grid;
var selectedItem = grid.BindingContext as Mapel;
var parent = grid.Parent as CollectionView;
((parent.Parent) as ScrollView).ScrollToAsync(grid, ScrollToPosition.MakeVisible, true);
foreach (var item in parent.ItemsSource)
{
var bg = item.FindByName<BoxView>("MainBg");
var details = item.FindByName<StackLayout>("DetailsView");
details.TranslateTo(-40, 0, 200, Easing.SinInOut);
bg.IsVisible = false;
details.IsVisible = false;
}
var selectionBg = grid.FindByName<BoxView>("MainBg");
var selectionDetails = grid.FindByName<StackLayout>("DetailsView");
selectionBg.IsVisible = true;
selectionDetails.IsVisible = true;
selectionDetails.TranslateTo(0, 0, 300, Easing.SinInOut);
AnimatedText(selectedItem.KKm);
}
在 .xaml
<Grid RowDefinitions="Auto, Auto, *" BackgroundColor="#F3F3F3" RowSpacing="20">
<ScrollView Grid.Row="1" VerticalOptions="Start" Orientation="Horizontal" Margin="30"
HorizontalScrollBarVisibility="Never">
<CollectionView ItemsLayout="HorizontalGrid" x:Name="mapelItem" VerticalOptions="Start" HeightRequest="100">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid HorizontalOptions="StartAndExpand" VerticalOptions="FillAndExpand">
<Grid.GestureRecognizers>
<TapGestureRecognizer Tapped="ItemTapped"/>
</Grid.GestureRecognizers>
<BoxView x:Name="MainBg" CornerRadius="20" BackgroundColor="{Binding Warna}" IsVisible="False"/>
<Grid ColumnDefinitions="Auto, Auto" ColumnSpacing="0" Padding="10">
<StackLayout x:Name="DetailsView" Grid.Column="1" VerticalOptions="Center" Padding="20,0"
TranslationX="-40" IsVisible="False">
<Label Text ="{Binding Simbol}" FontSize="16" TextColor="White" FontAttributes="Bold"/>
<Label Text ="{Binding KKm, StringFormat='{0:N0}'}" FontSize="14" TextColor="White"
FontAttributes="Bold"/>
</StackLayout>
<BoxView CornerRadius="20" HeightRequest="70" WidthRequest="70"
BackgroundColor="Black" Opacity="0.7"/>
<Label Text="book" HeightRequest="30" WidthRequest="30" FontFamily="FAS"
HorizontalOptions="Center" VerticalOptions="Center"/>
</Grid>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ScrollView>
<Grid Grid.Row="2" BackgroundColor="White" Padding="30">
<Label FontSize="70" FontAttributes="Bold" VerticalOptions="Center" HorizontalOptions="Center">
<Label.FormattedText>
<FormattedString>
<Span Text="$"/>
<Span Text="{Binding SelectedAmount, StringFormat='{0:N0}'}" TextColor="Black"/>
</FormattedString>
</Label.FormattedText>
</Label>
</Grid>
</Grid>