C# 如何无缝地将列表框刷新到底部?
我会尽力解释的。我正在尝试创建一个IM聊天应用程序,下面的代码将根据推送通知刷新列表框并向底部滚动。问题是每次刷新时,我都会看到所有记录从上到下快速闪烁几毫秒。如何才能使它在某种程度上,它是无缝的类似于手机短信C# 如何无缝地将列表框刷新到底部?,c#,windows-phone-8,listbox,C#,Windows Phone 8,Listbox,我会尽力解释的。我正在尝试创建一个IM聊天应用程序,下面的代码将根据推送通知刷新列表框并向底部滚动。问题是每次刷新时,我都会看到所有记录从上到下快速闪烁几毫秒。如何才能使它在某种程度上,它是无缝的类似于手机短信 private void getJSONObject(string jsonString) { if (jsonString != null && jsonString != "") { JObj
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();
}
}
}
private void getJSONObject(字符串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(“第一次命中”);
//如果存在数据,请运行以下命令
如果(a1.计数>0){
字符串消息;
字符串thisLastTimestamp=(字符串)a4[a4.Count-1];
//G.msgBox(“内部a1.count,外部if逻辑”);
if(lastTimestamp!=thisLastTimestamp&&isUserTyping==false)
{
IMChatData.removeElements();
lastTimestamp=此lastTimestamp;
对于(int i=0;i
问题是您一直在使用此行设置数据源lvIMChat.ItemsSource=IMChatData.collection
。您不需要这样做。只需设置一次源,然后更新基础数据
我不确定您的收藏类型是什么,但我建议使用
ObservableCollection
。当内容更改时,这将自动通知列表框
。我在Windows Phone 8中没有看到列表框的BeginUpdate?我还尝试过一次设置itemsSource,下次似乎不会刷新。我想我得到了一个w解决方法,但不确定这是否是最好的方法。我在代码末尾添加了lvIMChat.UpdateLayout();似乎可以阻止这个闪烁的问题。我猜UpdateLayout()是BeginUpdate()的精简.net版本您前面提到过。@Amaranks它可能不会刷新,因为您的集合没有实现INotifyPropertyChanged
在内容更改时通知控件。这就是为什么我建议使用ObservableCollection
。