本文介绍了Xamarin表单-获取在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表单-获取在Listview中选择的项目的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!