我是Xamarin的新手。我对Xamarin表单中的ListView有疑问。 每次我在listView中选项卡(或选择)时,如何知道哪一行或哪个索引?下面是我尝试过的代码,但e.SelectedItem没有显示任何内容。谢谢你的帮助。
listView.ItemSelected += async (sender, e) => {
if (e.SelectedItem == null) return; // don't do anything if we just de-selected the row
await DisplayAlert("Tapped", e.SelectedItem + " row was selected", "OK");
((ListView)sender).SelectedItem = null; // de-select the row
};
答案 0 :(得分:6)
我有一个列表视图,点击后我必须转到详细信息视图页面。以下是我用于相同的代码。
listView.ItemSelected += async (sender, e) =>
{
if (e.SelectedItem == null)
{
// don't do anything if we just de-selected the row
return;
}
else
{
Resource resource = e.SelectedItem as Resource;
listView.SelectedItem = null;
await Navigation.PushAsync(new ResourceDetails(resource));
}
};
在你的情况下,我会修改代码如下:
listView.ItemSelected += async (sender, e) => {
if (e.SelectedItem == null) return; // don't do anything if we just de-selected the row
await DisplayAlert("Tapped", (e.SelectedItem as YourDataType).Name + " row was selected", "OK");
((ListView)sender).SelectedItem = null; // de-select the row
};
答案 1 :(得分:0)
我找到了解决方法如何找到行的索引。 :)
listView.ItemSelected += async (sender, e) => {
if (e.SelectedItem == null) return; // don't do anything if we just de-selected the row
Person person = (Person)e.SelectedItem;
int index = -1;
for(int i = 0; i < people.Count; i++)
{
if(people[i] == person)
{
index = i;
break;
}
}
await DisplayAlert("Tapped", person.Name + " row was selected " + index.ToString(), "OK");
((ListView)sender).SelectedItem = null; // de-select the row
};
答案 2 :(得分:0)
更简单快捷的解决方案是在ItemSource上使用IndexOf - 在您的情况下&#34;人员&#34;
int index = people.IndexOf(person);
这将删除你的for循环