本文介绍了Xamarin Forms - 获取在 Listview 中选择的项目的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在尝试获取 ListView 中某个项目的位置时遇到了一些麻烦.
Im having a bit of trouble trying to get the position of an item in a ListView when its tapped.
这是我的代码:
public MainPage()
{
InitializeComponent();
Results();
}
async void Results()
{
var service = new RestService();
response = await service.GetEventItemsAsync();
Debug.WriteLine("Found: " + response.Count + " events");
listView.ItemsSource = response;
listView.ItemTapped += async (sender, e) =>
{
// Get position of item tapped
};
}
任何帮助将不胜感激.
已解决
必须将 ItemTapped 更改为 ItemSelected
然后在 ItemSelected 里面我刚刚在这一行中添加:
Then inside the ItemSelected I just added in this line:
var i = (listView.ItemsSource as List<EventItems>).IndexOf(e.SelectedItem as EventItems);
推荐答案
在 SelectedItem 事件中试试这个:
In SelectedItem event try this:
var index = (myListView.ItemsSource as List<MyObject>).IndexOf (e.SelectedItem as MyObject);
这篇关于Xamarin Forms - 获取在 Listview 中选择的项目的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!