我的列表视图中有一个开关,我选择了一些项目,如下面的截图。
我正在尝试获取切换项的用户标识。 我尝试如下,但获得例外:
public class MyToggledEventArgs : EventArgs
{
public UserProfileHBList MyItem { get; set; }
public MyToggledEventArgs(UserProfileHBList item)
{
this.MyItem = item;
}
}
void OnToggledEvent(object sender, MyToggledEventArgs args)
{
var item = args.MyItem;
if (item != null)
{
Debug.WriteLine("Userid:>>"+item.userProfileTO.userId);
}
}
//模型类
public class DirectoryResponse
{
public List<UserProfileHBList> userProfileHBList { get; set; }
}
public class UserProfileHBList
{
public UserProfileTO userProfileTO { get; set; }
}
public class UserProfileTO
{
public string userId { get; set; }
public string firstName { get; set; }
public string email { get; set; }
public string lastName { get; set; }
}
有没有其他方法可以获取用户ID?
答案 0 :(得分:2)
在OnToggledEvent(object sender, EventArgs args)
中,我们可以根据您的层次结构获得父ViewCell
:
public void OnToggledEvent(object sender, EventArgs args)
{
Here I use a Grid to wrap the content so I need to use two Parent to find the ViewCell
ViewCell cell = (sender as Switch).Parent.Parent as ViewCell;
// If you set the list<UserProfileHBList> as the ListView's ItemsSource, we can find the model through BindingContext
UserProfileHBList model = cell.BindingContext as UserProfileHBList;
// Then the userId can be known
if (model != null)
{
Debug.WriteLine("Userid:>>"+model.userProfileTO.userId);
}
}