0

當我在套接字上偵聽時,需要更新我的observableCollection。 然後將我的observableCollection從最大到最小排序。 我每次都在聽任何更新。 這工作正常。c#更新套接字中的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

首先,你不應該每次覆蓋你的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%的配件的答案,但它應該推你在正確的方向