c# 在套接字中更新 ObservableCollection
c# update ObservableCollection in socket
我需要在监听套接字时更新我的observableCollection
。
然后把myobservableCollection
从大到小排序。
我每次都在听任何更新。
这很好用。
但是当我向下滚动 longListSelector
时,**每次更新完成时,我都不能'
还没到尽头,它 returns 我到顶了。* *
如何更新它并能够向下滚动。
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);
}
}
}
如果您每次都覆盖集合,绑定的控件可能会遇到严重的绑定问题,因为它们永远不会解除绑定。
要滚动到特定元素,您可以这样做:
var lastMessage = playerCollection.LastOrDefault();
MainLongListSelector.ScrollTo(lastMessage);
这可能不会给您 100% 合适的答案,但它应该会把您推向正确的方向
我需要在监听套接字时更新我的observableCollection
。
然后把myobservableCollection
从大到小排序。
我每次都在听任何更新。
这很好用。
但是当我向下滚动 longListSelector
时,**每次更新完成时,我都不能'
还没到尽头,它 returns 我到顶了。* *
如何更新它并能够向下滚动。
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);
}
}
}
如果您每次都覆盖集合,绑定的控件可能会遇到严重的绑定问题,因为它们永远不会解除绑定。
要滚动到特定元素,您可以这样做:
var lastMessage = playerCollection.LastOrDefault();
MainLongListSelector.ScrollTo(lastMessage);
这可能不会给您 100% 合适的答案,但它应该会把您推向正确的方向