我会尽力解释。我正在尝试创建一个即时消息聊天应用程序,以下代码将根据推送通知刷新列表框并向下滚动。问题是,每次刷新时,我都会看到所有记录从上到下快速闪烁几毫秒。如何以一种与手机短信无缝相似的方式制作它?
private void getJSONObject(string jsonString)
{
if (jsonString != null && jsonString != "")
{
JObject o = JObject.Parse(jsonString);
JArray a1 = (JArray)o["a1"];
JArray a2 = (JArray)o["a2"];
JArray a3 = (JArray)o["a3"];
JArray a4 = (JArray)o["a4"];
//G.msgBox("1st hit");
//run the following if data exists
if(a1.Count>0){
String thisMessage;
String thisLastTimestamp = (string)a4[a4.Count - 1];
//G.msgBox("inside a1.count but outside if logic");
if (lastTimestamp != thisLastTimestamp && isUserTyping == false)
{
IMChatData.removeElements();
lastTimestamp = thisLastTimestamp;
for (int i = 0; i < a1.Count; i++)
{
thisMessage = HttpUtility.UrlDecode((string)a2[i]);
thisMessage = thisMessage.Replace("`", "'");
IMChatData.addElements(
(string)a1[i],
thisMessage,
(string)a3[i],
(string)a4[i]
);
}
//lvIMChat.ItemsSource = null;
lvIMChat.ItemsSource = IMChatData.collection;
//set scroll toward bottom after sending message
lvIMChat.ScrollIntoView(lvIMChat.Items[a1.Count - 1]);
lvIMChat.Focus();
}
}
}
答案 0 :(得分:0)
问题是您是否继续使用此行lvIMChat.ItemsSource = IMChatData.collection
设置数据源。你不应该这样做。设置源一次并更新基础数据。
我不确定你的收藏类型是什么,但我建议使用ObservableCollection<T>
。这将在内容更改时自动通知ListBox
。