[英]WP8 Item not found in LongListSelector
我試圖滾動到LongListSelector中的特定項目,但是當我調用llsTest.ScrollTo(m)函數時,我的longlistselector找不到它並崩潰了。
C#:
public class MyItem
{
public string s1 {get;set;}
public string z1 {get;set;}
}
List<MyItem> list= new List<MyItem>();
list.Add(new MyItem() { s1 = "First", z1 = "Second" });
list.Add(new MyItem() { s1 = "Third", z1 = "Fourth" });
list.Add(new MyItem() { s1 = "Fifth", z1 = "Sixth" });
list.Add(new MyItem() { s1 = "Sek8", z1 = "kj98" });
list.Add(new MyItem() { s1 = "lkdsj9", z1 = "lkdjo0" });
list.Add(new MyItem() { s1 = "jkdlhf", z1 = "98uifie" });
list.Add(new MyItem() { s1 = "Seventh11", z1 = "Eighth32" });
list.Add(new MyItem() { s1 = "Seventh45", z1 = "Eighth67" });
list.Add(new MyItem() { s1 = "Seventh86", z1 = "Eighth89" });
list.Add(new MyItem() { s1 = "Seventh6", z1 = "Eighth7" });
list.Add(new MyItem() { s1 = "Sevent4h", z1 = "Eighth8" });
list.Add(new MyItem() { s1 = "Seventh7i", z1 = "Eighthlp" });
list.Add(new MyItem() { s1 = "Seventh-09", z1 = "Eighth-0" });
list.Add(new MyItem() { s1 = "Seventh1q", z1 = "Eighthh65" });
list.Add(new MyItem() { s1 = "Second Last", z1 = "Last" });
MyItem m = new MyItem() { s1 = "Second Last", z1 = "Last" };
llsTest.ItemsSource = list;
llsTest.ScrollTo(m); // **<========Crashes here, m cannot be found!**
這是XAML:
<phone:LongListSelector Name="llsTest">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding s1}"/><LineBreak/>
<Run Text="{Binding z1}"/>
</TextBlock>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
MyItem m = new MyItem() { s1 = "Second Last", z1 = "Last" };
現在,在此之后,行m
不再添加到列表中。 因此很明顯,在嘗試滾動到不存在的項目時會拋出異常。
請注意,每次對
new
調用都會創建一個新對象,因此即使對象的內容相同,不同的對象也永遠不會相同。
所以對象傳入
list.Add(new MyItem() { s1 = "Second Last", z1 = "Last" });
與之后創建的對象不同。
MyItem m = new MyItem() { s1 = "Second Last", z1 = "Last" };
您需要在調用list.Add(m)
之前先調用llsTest.ScrollTo(m);
然后,您可以通過刪除行list.Add(new MyItem() { s1 = "Second Last", z1 = "Last" });
來刪除冗余元素list.Add(new MyItem() { s1 = "Second Last", z1 = "Last" });
而不是將新項目傳遞給ScrollTo,而是從列表數組中提供該項目。 我從代碼中看到要滾動到第15個項目。 因此,編寫如下代碼:
llsTest.ScrollTo(list[15]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.