Xamarin Forms PCL - Navbar Title wont update dynamically

I have made an app with a partial CarouselLayout.

In the layout when contentChanged I'm send a message with MessageCenter

      MessagingCenter.Send<CarouselLayout, string>(this, "ContentChanged", "123");

And in my RootPage I'm listening for it, and then i want to update the title:

MessagingCenter.Subscribe<CarouselLayout, string>(this, "ContentChanged", (sender, arg) =>
  {
    Debug.WriteLine("ContentChanged rootpage subscribe");
    UpdateDateTitle();
  });

public void UpdateDateTitle()
{
  this.Title  = _viewModel.CurrentPage.RegList.First().DateTime.ToString("D");
}

I can see on the debug output and the Title, that it is updated with the new date. However the actual navigationbar title is never updated.

But it is set on initial load.

What am i getting wrong?

Upvotes: 0

Views: 143

Answers (1)

Gerald Versluis
Gerald Versluis

Reputation: 33993

As discussed and discovered by yourself you should invoke this by using Device.BeginInvokeOnMainThread.

The MessagingCenter will use another thread and can't update the UI so you have to wrap put it on the UI thread.

It will look like this:

MessagingCenter.Subscribe<CarouselLayout, string>(this, "ContentChanged", (sender, arg) =>
{
   Device.BeginInvokeOnMainThread ( () => {
      Debug.WriteLine("ContentChanged rootpage subscribe");
      UpdateDateTitle();
   });
});

Read up on what it does exactly here.

Upvotes: 1

Related Questions