xamarin 如何使我的Carousel视图可点击?

ctehm74n  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(114)

我已经尝试了一段时间,使我的carousel视图可点击,并使用它来导航到其他页面/ shell 。

<CarouselView ItemsSource="{Binding Games}" HeightRequest="280" PeekAreaInsets="100">
            <CarouselView.ItemTemplate>
                    <DataTemplate x:DataType="viewmodels:NewGames">
                        <StackLayout>
                            <Frame HeightRequest="300" 
                                 WidthRequest="180" 
                                 BackgroundColor="white" 
                                 Padding="0" 
                                 CornerRadius="10"
                                 HasShadow="True" 
                                 Margin="15"
                                 HorizontalOptions="CenterAndExpand">
                                <Grid>
                                    <StackLayout BackgroundColor="DimGray">
                                    <Image Source="{Binding ImageSource}" Aspect="AspectFill">
                                    </Image>
                                    </StackLayout>
                                <StackLayout Margin="-5">
                                    <Label Text="{Binding GameTitle}" 
                                         TextColor="PaleGoldenrod"
                                         FontSize="18" 
                                         FontAttributes="Bold"
                                         Margin="15"
                                         VerticalOptions="EndAndExpand"/>
                                </StackLayout>
                            </Grid>
                            </Frame>
                        </StackLayout>
                    </DataTemplate>
            </CarouselView.ItemTemplate>
        </CarouselView>

我在carousel view选项卡下查看了xamarin文档,但没有找到任何有用的东西。

pbwdgjma

pbwdgjma1#

您可以尝试添加一个TapGestureRecognizerFrame,如Jason建议如下。

    • Xaml:**
<CarouselView ItemsSource="{Binding Games}" HeightRequest="280" PeekAreaInsets="100">
            <CarouselView.ItemTemplate>
                    <DataTemplate x:DataType="viewmodels:NewGames">
                        <StackLayout>
                            <Frame HeightRequest="300" 
                                 WidthRequest="180" 
                                 BackgroundColor="white" 
                                 Padding="0" 
                                 CornerRadius="10"
                                 HasShadow="True" 
                                 Margin="15"
                                 HorizontalOptions="CenterAndExpand">
                                
                              <Frame.GestureRecognizers>
                                     <TapGestureRecognizer 
                                         Tapped="TapGestureRecognizer_Tapped">
                                     </TapGestureRecognizer>
                             </Frame.GestureRecognizers>

                                <Grid>
                                    <StackLayout BackgroundColor="DimGray">
                                    <Image Source="{Binding ImageSource}" Aspect="AspectFill">
                                    </Image>
                                    </StackLayout>
                                <StackLayout Margin="-5">
                                    <Label Text="{Binding GameTitle}" 
                                         TextColor="PaleGoldenrod"
                                         FontSize="18" 
                                         FontAttributes="Bold"
                                         Margin="15"
                                         VerticalOptions="EndAndExpand"/>
                                </StackLayout>
                            </Grid>
                            </Frame>
                        </StackLayout>
                    </DataTemplate>
            </CarouselView.ItemTemplate>
        </CarouselView>
    • 代码隐藏:**
private async void TapGestureRecognizer_Tapped(object sender, EventArgs e) 
{
    //navigate to other pages/shells
     await Navigation.PushModalAsync(new Page1());
}

相关问题