У меня есть Xamarin Forms ListView
настроить как этотПолучение содержимого из ListView
public class AddressView : ContentPage
{
public AddressView()
{
this.Title = "Native address book";
CreateView();
}
async void CreateView()
{
IAddress addresses = DependencyService.Get<IAddress>();
var addressList = addresses.ContactDetails();
if (addressList.Count == 0)
{
await DisplayAlert("No contacts", "Your phone has no contacts stored on it", "OK");
return;
}
if (Device.OS != TargetPlatform.iOS)
BackgroundColor = Color.White;
else
Padding = new Thickness(0, 20, 0, 0);
var myList = new ListView()
{
ItemsSource = addressList,
ItemTemplate = new DataTemplate(typeof(MyLayout))
};
myList.ItemSelected += MyList_ItemSelected;
Content = myList;
}
void MyList_ItemSelected (object sender, SelectedItemChangedEventArgs e)
{
var item = e.SelectedItem as ViewCell;
}
}
public class MyLayout : ViewCell
{
public MyLayout()
{
var label = new Label()
{
Text = "name",
Font = Font.SystemFontOfSize(NamedSize.Default),
TextColor = Color.Blue
};
var numberLabel = new Label()
{
Text = "number",
Font = Font.SystemFontOfSize(NamedSize.Small),
TextColor = Color.Black
};
this.BindingContextChanged += (object sender, EventArgs e) =>
{
var item = (KeyValuePair<string,string>)BindingContext;
label.SetBinding(Label.TextProperty, new Binding("Key"));
numberLabel.SetBinding(Label.TextProperty, new Binding("Value"));
};
View = new StackLayout()
{
Orientation = StackOrientation.Vertical,
VerticalOptions = LayoutOptions.StartAndExpand,
Padding = new Thickness(12, 8),
Children = { label, numberLabel }
};
}
}
Это берет адресную книгу с родной платформой (и работает отлично). То, что я пытаюсь сделать, это прочитать два свойства Text из меток в ViewCell в DataTemplate.
Есть ли способ перебрать через дочерние объекты в ViewCell, чтобы найти значения меток внутри ячейки?