I am trying to scroll to a particular item in LongListSelector, but my longlistselector cannot find it and crashes when I call the llsTest.ScrollTo(m) function.
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!**
Here is the 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" };
now after this above line m
is never added to list. so obviously it will throw an exception while trying to scroll to a non existing item.
Note that, every call to
new
creates a new object, so even if the content of objects are same, different objects would never be the same.
so object passed in to
list.Add(new MyItem() { s1 = "Second Last", z1 = "Last" });
isn't same as object created afterwards.
MyItem m = new MyItem() { s1 = "Second Last", z1 = "Last" };
u need to call list.Add(m)
before call to llsTest.ScrollTo(m);
u can then remove a redundant element by removing line list.Add(new MyItem() { s1 = "Second Last", z1 = "Last" });
Instead of passing a new item to ScrollTo, give the item from the list array. I see from the code that you want to scroll to the 15th item. So write the code like below:
llsTest.ScrollTo(list[15]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.