当我在套接字上侦听时,我需要更新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;
});
});
答案 0 :(得分:0)
首先,每次都不应覆盖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%合适的答案,但它应该会让你朝着正确的方向前进