Xamarin.Forms adding items/data from a Rest API to an ObservableCollection

faceoffers28faceoffers28 USUniversity ✭✭✭

I've got a Xaml page that I'm using ListView on to display a list of offers. I'm trying to Get offers using a REST Web API call to display on the page using an ObservableCollection. Since async calls don't seem to work on ObservableCollection methods, I've moved that to a public async task method under the public ObservableCollection GetOffers() method. Here is my code.

public class OffersService : IOffersService

        public ObservableCollection<OffersModel> GetOffers()
            var offersList = new ObservableCollection<OffersModel> ();

            var offers = GetOffersAsyncOC ();

            OffersModel model = new OffersModel () {

                Id = Guid.NewGuid (),
                OfferImage = null,
                OfferType = "Dollar Off",
                Name = "Test name",
                Description = "Test Description",
                Amount = 25,
                StartDate = DateTime.Now.Date,
                EndDate = DateTime.Now.Date.AddHours (8),


            offersList.Add (model);         
            return offersList;

As you can see, I'm using the GetOffersAsyncOC method to get a list of offers. I'm then manually adding an offer for testing purposes because I can't seem to find a way to add the offers from the Rest API to the ObservableCollection. For testing purposes, this works, but I need to be able to work with data coming from the Rest API.

Any help is much appreciated. I've been developing for less than a year, so I may need a little more explanation than usual. Thanks!


  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭

    Do you have any code to get the offers from the REST API yet?

    I can imagine you would have to edit your code to do something like this (untested from the top of my head);

    public class OffersService : IOffersService
       public async void SomeInitMethod()
         // Here you should probably add it to some property that you data-bind
          var itemSource = await GetOffers();
       public async Task<ObservableCollection<OffersModel>> GetOffers()
          new ObservableCollection<OffersModel> offersList;
          // This is the call too your API?
          var offers = await GetOffersAsyncOC ();
          offersList = new ObservableCollection<OffersModel>(offers);
          return offersList;

    Hope this helps :)

  • faceoffers28faceoffers28 USUniversity ✭✭✭

    @GeraldVersluis This is really helpful.

    Here is how I am referencing the current method from the IOffersService.

    public interface IOffersService
            ObservableCollection<OffersModel> GetOffers();

    Here is my method for getting the offers from the API.

    public async Task GetOffersAsyncOC()
                string faceoffersApiKey = "SOMEAPIKEY";
                FaceOffersService service = new FaceOffersService(faceoffersApiKey);
                IEnumerable<FaceOffersOffer> offers = await service.Offer.List(service.AuthToken.Token, service.MerchantID);

    Does this change anything in terms of what you have already suggested?

    Thanks ever so much!

Sign In or Register to comment.