Xamarin формы: как получить доступ к идентификатору переключаемых элементов с помощью OnToggledEvent - PullRequest
0 голосов
/ 01 мая 2018

В моем списке просмотра есть переключатель, и я выбираю некоторые элементы, как показано на скриншоте ниже.

enter image description here

Я пытаюсь получить идентификаторы переключенного элемента. Я пытаюсь, как показано ниже, но получаю исключение:

 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; }
}

Есть ли другой способ получить идентификатор пользователя?

1 Ответ

0 голосов
/ 02 мая 2018

В 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);
    }

}
...