【发布时间】:2021-06-29 11:00:49
【问题描述】:
我正在尝试创建一个聊天页面。有一个列表视图,其 itemssource 是 ObservableCollection。在将项目添加到 ObservableCollection 之前,一切似乎都很顺利。添加的项目不会立即出现在屏幕上,只有在触摸屏幕后才会显示。
事实上,我也有同样的问题:https://forums.xamarin.com/discussion/18631/listview-binding-to-observablecollection-does-not-update-gui/p2
我相信线程(第 2 页)中所述的解决方案之一与 INotifyPropertyChanged 有关。经过多次尝试应用解决方案以满足我的需求后,我无法这样做。所以请您指导我正确的方向。
我的代码总结如下:
public partial class Chat : ContentPage
{
private ObservableCollection<MessageObj> Messages = new ObservableCollection<MessageObj>();
public Chat(string name, string imageURL, int UID)
{
msgList.ItemsSource = Messages; //msgList is ListView in XAML
}
public class MessageObj
{
public string Mess { get; set; }
public TextAlignment textAlignment { get; set; }
public MessageObj(string Mess, TextAlignment textAlignment)
{
this.Mess = Mess;
this.textAlignment = textAlignment;
}
}
}
我还阅读过:Xamarin Forms ListView ObservableCollection not updating 我想我正在更改收藏,所以我不应该面对这个问题,但我确实是。 任何帮助/见解将不胜感激!
我添加了一个包含我的代码以供参考的要点,其中显示了我现在评论过的不同解决方案的尝试:https://gist.github.com/Dobermensch/49ee9d8adb9a83a38790b691c857691d
编辑:在要点中添加了一些最初省略的代码
Edit2:仅供参考,我没有使用 MVVM 模式。
【问题讨论】:
-
MessageObj 应该继承自
Bindable -
在该代码中我没有看到任何将数据添加到 Messages 集合的尝试
-
我在 OnAppearing() 函数 LoadChat() 的 Messages 集合中添加数据
-
能否详细说明 1SaeedSalehi?我什至无法从那段简短的声明中想到要在谷歌上搜索什么。
-
@ayudh 将
public class MessageObj更改为public class MessageObj : Bindable,因为需要监控 BindableProperies 的更改或集合更改
标签: listview xamarin xamarin.forms xamarin.android sendbird