我正在学习Xamarin,我想在我的CarouselView中获取被轻击的元素的idex
这里是Xaml代码:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
x:Name="currentPage">
<StackLayout >
<CarouselView x:Name="TestList" >
<CarouselView.ItemTemplate >
<DataTemplate>
<StackLayout >
<StackLayout Grid.Row="3" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding CarouselItemTapped,Source={x:Reference currentPage}}" CommandParameter="{Binding .}"/>
</StackLayout.GestureRecognizers>
</StackLayout>
</StackLayout>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
</StackLayout>
这是我的C#代码:
初始化:
public ICommand CarouselItemTapped { get; set; } // initialize my command
new List<TestModel> ListWordsTest = new List<TestModel>(); // initialize my list of TestModel
// the Model I use get strings elements declared as Public {get; set;}
TestModel testmodel = new TestModel
{
Word1 = "word1", // type string
Word2 = "word2", //type string
};
获取点击的元素信息
//When I tapped The carousel I get information like this
CarouselItemTapped = new Xamarin.Forms.Command((selectItem) => {
var mynews = selectItem //as IDoNotKnow I would like to get the item index
});
Position属性将告诉您可见项目的索引,而CurrentItem是实际项目。
假设ListWordsTest
是您的轮播视图的商品来源,然后我们可以通过以下方式获取索引:
CarouselItemTapped = new Xamarin.Forms.Command((selectItem) =>
{
var mynews = selectItem; //as IDoNotKnow I would like to get the item index
var index = ListWordsTest.IndexOf((TestModel)selectItem);
});