Vario
Vario

Reputation: 530

WPF Listview with ObserveableCollection does not update from UdpClient but updates from timer

I tried several suggested solutions now, but I'm stuck.

I have a WPF MVVM application, which should list all strings passed through a UDP Client and updates it.

I dont know why but my collection gets updated and the Listview does not update. The code works fine, as long as I add values somehow through application code (button click) or a timer which adds a message each second.

The weird thing now is, that if I do both simultaneously, only the timer messages are shown in the ListView, but NOT the UDP messages, although both are in the ObserveableCollection

Here is a Screenshot of the application on the right and the Collection on the left. MessageXX are from the timer which represent each second. All others are added through UDP Update

To make it easier I switched my LogEvent Object to a simple ObserveableCollection type, but nothing changed. The AddLogEntry method is called from the UDP Client and the Timer.

enter image description here

This is how I initialize the MainViewModel and UDP Client

public MainViewModel()
{
    LogEntries = new ObservableCollection<String>();
    udpClient = GetUDPClient();
    StartListeningAsync();
    StartTMPEntries();
}

private UdpClient GetUDPClient()
 {
     if(udpClient == null)
     {
         udpClient = new UdpClient();
         udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
         udpClient.Client.Bind(getIPEndPoint());
     }   
     return udpClient;
 }

This is the method to start the update and update the collection (from UDP and timer)

 private async void StartListeningAsync()
 {
     
     while (true)
     {
         UdpReceiveResult result = await udpClient.ReceiveAsync();
         string message = Encoding.UTF8.GetString(result.Buffer);
         await Application.Current.Dispatcher.InvokeAsync(() =>
         {
             AddLogEntry(message);
         });
     }
 }
 private async void AddLogEntry(string message)
 {
   await Application.Current.Dispatcher.InvokeAsync(() =>
   {
      Debug.WriteLine(message);
      LogEvent logEvent = new LogEvent(message);
      LogEntries.Add(logEvent.Message);
      OnPropertyChanged(nameof(LogEntries));
      var listView = Application.Current.MainWindow.FindName("ListView") as System.Windows.Controls.ListView;
      if (AutoScroll && LogEntries.Any())
      {
          listView?.ScrollIntoView(LogEntries.Last());
      }
  });
 }

This is the ListView Part

 <ListView ItemsSource="{Binding LogEntries}" SelectedItem="{Binding SelectedLogEntry}" Name="ListView">
 </ListView>

Timer methods which work and are called after initializing UDP. Temporarily modified it to not use the LogEvent in the AddLogEntry, just pass a string for now.

private void StartTMPEntries()
{
    timer = new Timer(1000); // Set up the timer for 1 second intervals
    timer.Elapsed += OnTimedEvent; // Hook up the Elapsed event for the timer
    timer.AutoReset = true;
    timer.Enabled = true; // Start the timer
}
private async void OnTimedEvent(Object source, ElapsedEventArgs e)
{
    await Application.Current.Dispatcher.InvokeAsync(() =>
    {
        LogEvent logEvent = new LogEvent();
        DateTime epochStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        logEvent.Timestamp = (long)(DateTime.Now.ToUniversalTime() - epochStart).TotalMilliseconds;
        AddLogEntry("Message" + DateTime.Now.Second);
    });
}

Any ideas what could cause this problem or what I could try?

Thank you very much

Upvotes: 0

Views: 44

Answers (1)

Vario
Vario

Reputation: 530

Found the issue and it was quite silly... I have defined the VM twice, in the XAML and Code behind... After deleting the xaml part, everything worked as expected.

<Window x:Class="xxx.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:vm="clr-namespace:xxx.ViewModels"
    Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
    <vm:MainViewModel />
</Window.DataContext>
<Grid>
......

public MainWindow()
{
    InitializeComponent();
    DataContext = new MainViewModel();
}

Upvotes: 0

Related Questions