在Xamarin中无法在ListView上获得Item Tap事件

时间:2018-08-11 11:45:53

标签: c# xamarin xamarin.forms xamarin.ios xamarin.android

我正在尝试在xamarin中获得ListView项的click或Tap事件,但我不知道自己缺少什么。

.Xaml文件代码:

<ListView x:Name="MyListView" ItemTapped="MyItemTapped" ItemSelected="MyItemSelected"
            ItemsSource="{Binding Items}"
          HasUnevenRows="True"
                      SeparatorVisibility="Default"
                      HorizontalOptions="FillAndExpand"
                      Grid.Row="3"
                      SeparatorColor="#efeff4"
            CachingStrategy="RecycleElement" >



                        <!--Custom View Cells-->

                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <local:addcontainer>
                                        <local:addcontainer.Content>
                                            <Grid BackgroundColor="#efeff4"   x:Name="grid"  VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" >

                                                <StackLayout Orientation="Horizontal" Padding="8,6,8,10"   BackgroundColor="#f9f9fb"  HorizontalOptions="FillAndExpand"  VerticalOptions="FillAndExpand">
                                                    <ffimageloading:CachedImage HorizontalOptions="Start" VerticalOptions="FillAndExpand" LoadingPlaceholder="loading.png" ErrorPlaceholder="error.png" DownsampleToViewSize="true"
                                                    Aspect="AspectFit" HeightRequest="90" WidthRequest="90" Source="{Binding Image}" >
                                                        <ffimageloading:CachedImage.Transformations>
                                                            <fftransformations:CircleTransformation/>
                                                        </ffimageloading:CachedImage.Transformations>
                                                    </ffimageloading:CachedImage>
                                                    <StackLayout Orientation="Vertical" VerticalOptions="FillAndExpand" HorizontalOptions="Start" >
                                                        <Label Text="{Binding Name}" TextColor="#253045" FontSize="16" FontAttributes="Bold" FontFamily="SFProDisplay-Bold.ttf"
                                                   HorizontalOptions="Start" VerticalOptions="Start" >
                                                            <Label.FontFamily>
                                                                <OnPlatform x:TypeArguments="x:String">
                                                                    <OnPlatform.Android>SFProDisplay-Bold.ttf#SF-Pro-Display-Bold</OnPlatform.Android>
                                                                    <OnPlatform.iOS>SFProDisplay-Bold</OnPlatform.iOS>
                                                                </OnPlatform>
                                                            </Label.FontFamily>

                                                        </Label>

                                                        <StackLayout Orientation="Horizontal" VerticalOptions="Start" HorizontalOptions="StartAndExpand" Padding="0,-2,0,0" >
                                                            <Image Source="location.png" WidthRequest="9" HeightRequest="9" Aspect="AspectFit"></Image>
                                                            <Label  Text="{Binding Address}"  FontSize="12" TextColor="#9495a1"  HorizontalTextAlignment="Start" VerticalTextAlignment="Start"
                                                >
                                                                <Label.FontFamily>
                                                                    <OnPlatform x:TypeArguments="x:String">
                                                                        <OnPlatform.Android>SFProDisplay-Medium.ttf#SFProDisplay-Medium</OnPlatform.Android>
                                                                        <OnPlatform.iOS>SFProDisplay-Medium</OnPlatform.iOS>
                                                                    </OnPlatform>
                                                                </Label.FontFamily>
                                                            </Label>
                                                        </StackLayout>
                                                        <rating:SfRating  x:Name="rating" Value="{Binding Rating}" ItemCount="5" ItemSize="14" Margin="0,0,0,1">
                                                            <rating:SfRating.RatingSettings>
                                                                <rating:SfRatingSettings RatedFill="#f3b71e"  UnRatedFill="#9e9e9e" RatedStrokeWidth="0" UnRatedStrokeWidth="0" />
                                                            </rating:SfRating.RatingSettings>
                                                        </rating:SfRating>
                                                        <Label Text="{Binding Services}" FontFamily="SFProDisplay Medium" TextColor="#253045" FontSize="12" 
                                                   HorizontalOptions="Start"  Margin="0,-2,0,3">
                                                            <Label.FontFamily>
                                                                <OnPlatform x:TypeArguments="x:String">
                                                                    <OnPlatform.Android>SFProDisplay-Medium.ttf#SFProDisplay-Medium</OnPlatform.Android>
                                                                    <OnPlatform.iOS>SFProDisplay-Medium</OnPlatform.iOS>
                                                                </OnPlatform>
                                                            </Label.FontFamily>
                                                        </Label>
                                                    </StackLayout>

                                                </StackLayout>
                                                <StackLayout HorizontalOptions="FillAndExpand" HeightRequest="4" BackgroundColor="#efeff4" VerticalOptions="EndAndExpand" Padding="0">

                                                </StackLayout>

                                            </Grid>


                                        </local:addcontainer.Content>


                                    </local:addcontainer>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>

                    </ListView>

和带有两个事件处理程序方法的xaml.cs文件:

void MyItemTapped(object sender, ItemTappedEventArgs e)
{
    DisplayAlert("Alert", "You Pressed Something!", "OK");
}

void MyItemSelected(object sender, ItemTappedEventArgs e)
{
    DisplayAlert("Alert", "You Selected Something!", "OK");
}

但是当我在两个函数上都击中Debugger时,却没有一个被触发。

0 个答案:

没有答案