I am trying to create a chatting page. There's a listview whose itemssource is an ObservableCollection. Everything seems to go smoothly until an item is added to the ObservableCollection. The added item does not immediately appear on the screen and only after touching the screen does it show up.
In fact, I have the same issue as stated here: https://forums.xamarin.com/discussion/18631/listview-binding-to-observablecollection-does-not-update-gui/p2
I believe one of the solutions stated in the thread (Page 2) has something to do with INotifyPropertyChanged. After many attempts of trying to apply the solution to fit my needs, I am unable to do so. So could you please guide me in the correct direction.
My code is summarized as follows:
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;
}
}
}
I've also read: Xamarin Forms ListView ObservableCollection not updating I think I am changing the collection so I shouldn't be facing this issue but I am. Any help/insight would be greatly appreciated!
I have added a gist containing my code for reference which shows my now commented attempts at different solutions: https://gist.github.com/Dobermensch/49ee9d8adb9a83a38790b691c857691d
Edit: Added a little bit of more initially omitted code to gist
Edit2: FYI, I am not using the MVVM pattern.
Bindable
– 1SaeedSalehipublic class MessageObj
topublic class MessageObj : Bindable
because BindableProperies needs to be monitored for changes or collection changes – 1SaeedSalehi