I'm trying to make a UI with list of data(measurements) that changes every second.
I have a model:
namespace DebugApp.Model;
public class Channel
{
public byte Id { get; set; }
public string FwVersion { get; set; }
public int Measurement1{ get; set; }
public int Measurement2 { get; set; }
}
and a list of data in ViewModel:
namespace DebugApp.ViewModel;
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private ObservableCollection<Channel> channelInfo = new ObservableCollection<Channel>();
public ObservableCollection<Channel> ChannelInfo
{
get => channelInfo;
set
{
OnPropertyChanged();
}
}
public void OnPropertyChanged([CallerMemberName] string name = null) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
The init of UI:
namespace DebugApp.View;
public partial class MainPage : ContentPage
{
ObservableCollection<Channel> channelInfo = new ObservableCollection<Channel>();
ViewModel viewModel = new ViewModel();
public MainPage()
{
InitializeComponent();
..
}
..
To initially make the first display of data in the list, I call this function:
private void GetAllChannelsInfo()
{
ushort[] registerContent;
channelInfo.Clear();
for (byte channel = 1; channel <= myReader.noOfConnectedChannels; channel++)
{
registerContent = myReader.ReadRegisters(channel, myReader.RegVersionMajor, 7);
if (registerContent != null)
{
channelInfo.Add(new Channel()
{
Id = channel,
FwVersion = registerContent[0].ToString() + "." +
registerContent[1].ToString() + "." +
registerContent[2].ToString(),
Meas1 = (Int16)registerContent[5],
Meas2 = (Int16)registerContent[6],
});
}
}
viewModel.ChannelInfo = channelInfo;
BindingContext = this.viewModel;
}
In the XAML file:
..
xmlns:viewmodel="clr-namespace:DebugApp.ViewModel">
..
<ContentPage.BindingContext>
<viewmodel:ViewModel/>
</ContentPage.BindingContext>
..
<ListView ItemsSource="{Binding ChannelInfo}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Spacing="5"
Orientation="Horizontal">
<Label Text="{Binding Id,
StringFormat='Channel {0}: '
}"
WidthRequest="85"/>
<Label Text="{Binding FwVersion,
StringFormat='FwVer: {0}, '
}"
WidthRequest="100"/>
<Label Text="{Binding Meas1,
StringFormat='M1: {0}, '
}"
WidthRequest="140"/>
<Label Text="{Binding Meas2,
StringFormat='M2: {0}'
}"
WidthRequest="140"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
This works and the data are shown, but then after the initialization of the table/list, then once a second I a function where I only change the value of channelInfo[x].Meas1 and Meas2:
private void GetChannelsUpdate()
{
..
channelInfo[channel].Meas1 = someNumber;
channelInfo[channel].Meas2 = someOtherNumber;
..
viewModel.ChannelInfo = channelInfo;
BindingContext = this.viewModel;
}
But the data are not being updated in my UI. I have tried to debug this and can see that the data are being updated in ViewModel and the OnPropertyChanged() are called, but the data in the UI are not changed.
So, why aren't the List being updated when the data inside are changed?