我需要通过索引获取可观察集合中的对象,以访问该索引处项目的属性。
这是代码的片段:
public ObservableCollection<TipsModel> TipObjects;
private void LoadContent()
{
TipObjects = new ObservableCollection<TipsModel>();
for (int i = 0; i < 5; i++)
{
TipsModel item = new TipsModel()
{
Image = ImageSource.FromFile("nonindustryIcon.png"),
Title = "Kill energy vampires and save up to $100 a year",
Text = "Seventy-five percentof the electrical use by home electronics occurs when they're at home. \n People not at home means no electricity. Do not stay at home. Go stay on the streets. ",
};
TipObjects.Add(item);
}
foreach (TipsModel item in TipObjects)
{
img = item.Image;
tipTitle = item.Title;
tip = item.Text;
item.Content = CreateContent();
}
slideView.ItemsSource = TipObjects;
}
private void slideView_SlidedToIndex(object sender, Telerik.XamarinForms.Primitives.SlideView.SlideViewSlidedToIndexEventArgs e)
{
var slideId = slideView.Id;
//TipsModel tip = TipObjects.item at index[18];
}
答案 0 :(得分:2)
你通常可以做到
var tip = TipObjects[18];
Observable collection只是一个支持索引的普通,功能齐全的集合。
或者您也可以使用Items
属性。两种方法都是等价的:
var tip = TipObjects.Items[18];