MessagingCenter callback executes viewmodel's method but doesn't update UI

ExoDevExoDev USMember ✭✭
edited December 2017 in Xamarin.Forms

i subscribe at a MessageCenter instance on my MainPage like that:

MessagingCenter.Subscribe<App, int>(this, App.TIMER_TICK_ACTION, (sender, arg) => {
       Device.BeginInvokeOnMainThread(() =>
       {
             TimerTickAction(arg);
       });
 });

private void TimerTickAction(int counter)
{
    try
    {
          if ((counter % 5) == 0)
          {
               matchViewModel.UpdateLiveMatches();
               System.Diagnostics.Debug.WriteLine("Update");
          }
          else
          {
               matchViewModel.TimeTick();
               System.Diagnostics.Debug.WriteLine("Tick");
           }
     }
     catch (Exception ex)
     {

         System.Diagnostics.Debug.WriteLine(ex.Message);
     }
}

The Send<> is executed on App.cs class every 1 sec :

Device.StartTimer(new TimeSpan(0,0,1), () =>{
      counter++;
      MessagingCenter.Send<App, int>(this, TIMER_TICK_ACTION, counter);
      return pageIsActive;
});

The viewmodel's methods TimeTick and UpdateLiveMatches are getting executed successfully. I set breakpoints and i see that the all blocks inside the ViewModel class are getting executed and setting log prints the updated data. But the UI is not getting updated. Like if the binding mechanism is not working. The strange thing is that if i StartTimer on MainPage() and start executing the exact same TimerTickAction(arg) method, is it successfully updating the UI.

Conclusion

Executing View Model method from MessagingCenter labmda is not updating UI.

Any help / explanation would be greatly appreciated,
Thanks in advance

EDIT: Please not that my binding is tested and working fine. The methods are the same. The only thing that changes is the scope they're being called

Sign In or Register to comment.