observableCollection
当我在套接字上收听时,我需要更新我的内容。然后observableCollection
从最大到最小对我进行排序。我每次都在听任何更新。这很好。
但是,当我向下滚动我的longListSelector
**每次完成更新时,我都无法到达结尾,它使我回到顶部。* *如何更新它并能够向下滚动。
mySock.On("player:new-vote", (data) = > {
string newVoteData = data.ToString();
JObject obj = JObject.Parse(newVoteData);
string objId = (string) obj["id"];
int objStand = (int) obj["details"]["standing"];
int objUp = (int) obj["details"]["upVotes"];
int objDown = (int) obj["details"]["downVotes"];
string objBy = (string) obj["details"]["by"];
PlayerVotes newVote = new PlayerVotes() {
by = objBy,
_id = objId,
downVotes = objDown,
upVotes = objUp,
standing = objStand
};
Deployment.Current.Dispatcher.BeginInvoke(() = > {
var updateVoteSong = playerCollection.FirstOrDefault(x = > x._id == objId);
updateVoteSong.votes = newVote;
playerCollection = new ObservableCollection < PlayerSong > (playerCollection
.OrderByDescending(x = > x.votes.standing));
MainLongListSelector.ItemsSource = playerCollection;
});
});
首先,您不应每次都覆盖您的ObservableCollection,因为包含的数据会发生变化。
而是使用此扩展名进行排序:
public static class ObservableCollection
{
public static void Sort<TSource, TKey>(this ObservableCollection<TSource> source, Func<TSource, TKey> keySelector)
{
List<TSource> sortedList = source.OrderByDescending(keySelector).ToList();
source.Clear();
foreach (var sortedItem in sortedList)
{
source.Add(sortedItem);
}
}
}
如果您每次都重写Collection,则绑定控件可能会遇到严重的绑定问题,因为它们从未解除绑定。
并滚动到一个特定的元素,您可以这样:
var lastMessage = playerCollection.LastOrDefault();
MainLongListSelector.ScrollTo(lastMessage);
这可能无法为您提供100%合适的答案,但应将您推向正确的方向
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句