John554
John554

Reputation: 303

Xamarin Forms Update Page Content

I have a Xamarin forms app and I am trying to update the content on one of the pages from the output of an API call without refreshing the page.

Where would I put a loop to make the API call and update the view model every X seconds? I can run all the API calls I want in the constructor but I can't seem to find a way to have this happen once the page is loaded.

Upvotes: 0

Views: 326

Answers (1)

Alexandar May - MSFT
Alexandar May - MSFT

Reputation: 10073

If you want to update the content on one of the pages,you could put the code inside the timer to interact on the UI thread, it should be done within a BeginInvokeOnMainThread expression, which will be nested inside the timer (see below). You could refer to below sample code:

Device.StartTimer (new TimeSpan (0, 0, 60), () =>
{
    // do something every 60 seconds
    Device.BeginInvokeOnMainThread (() => 
    {
      // interact with UI elements
    });
    return true; // runs again, or false to stop
});

MS official reference link.

Upvotes: 1

Related Questions