Xamarin.Forms quits unexpectedly on adding element to oberservable collection

Hello, I am using an observalbe collection as a template holder for a Carousel View. When the user gets to the end of the loaded pages, and update is triggered that fetches more data from the server. When I construct a new page template from the data and attempt to add it to the collection, the app just quits suddenly, with no exception being thrown or other indication of what happened.

I tried seeing if I could catch the error in try catch block, but no luck. As soon as I single step into (or over) the Add method (see code below) single step the app just stops immediately returns to the home page.

private ObservableCollection<ResultsScroller> mySource;
public void PopulateResultsPages(List<NominalResult> resultList)
  {
    foreach (var nr in resultList)
    {
      var template = new ResultsScroller();
      template.LoadData(nr);
      try
      {   
        mySource.Add(template);   //  <---------------Failure occurs here.  
      }
       catch (System.Exception e)
     {
     System.Diagnostics.Debug.WriteLine(e.Message);
    }
  }
  lastPosition = mySource.Count - 1;
}

Note I recently updated all my Packages, if that has anything to do with it.

Answers

  • ShantimohanElchuriShantimohanElchuri USMember ✭✭✭✭✭

    @joshKramer I don't see anything wrong with the code. So even the catch is not catching the exception. Then I am thinking wild...is template null after the execution of LoadData method?

  • JohnHardmanJohnHardman GBUniversity mod

    @JoshKramer - If you run your code (debug build) from Visual Studio, what appears in the Output window when the exception is thrown? It may be that the exception is happening elsewhere, such as in anything notified of the change to the ObservableCollection.

    Is PopulateResultsPages called from the UI thread? If not, if you have UI views bound to ObservableCollection, then the exception may be as a result of trying to update the UI from a non-UI thread.

Sign In or Register to comment.