title: Binding ReactiveList<T> to ListView in Xamarin Forms
body: <p>I feel like I am doing this the right way, but am unsure. I am loading objects into a ReactiveList in my ViewModel asynchronously. Through Xamarin Forms, I have bound the List to the ItemSource property of a ListView in Xamarin Forms. I also have a search box which will clear the ReactiveList and add new items to the ReactiveList.</p>
  
  <p>The first time I open the View, no interaction makes the list load, and a button that is bound to a ReactiveCommand for loading more items into the ReactiveList is disabled.</p>
  
  <p>The second time I open the View the ListView renders the items in the new ViewModel almost immediately, but again, interactions do not seem to work. However, changing the search box does actually clear items from the ListView, but items do not get added.</p>
  
  <p>This behavior is on iOS. I am ReactiveUI 6.3.1 and Xamarin Forms 1.3.2.6316.</p>
  
  <p>Here is the simplified problem:</p>
  
  <pre><code>public class MyViewModel : ReactiveObject, IRoutableViewModel
  {
      public MyViewModel(IScreen hostScreen = null)
      {
          HostScreen = hostScreen ?? Locator.Current.GetService&lt;IScreen&gt;();
  
          List = new ReactiveList&lt;ItemViewModel&gt;()
          {
              ChangeTrackingEnabled = true
          };
  
          //This never gets shown
          List.Add(new ItemViewModel()
          {
              Name = "TEST"
          });
  
          //Tried doing this on the main thread: same behavior
          LoadItemsCommand = ReactiveCommand.CreateAsyncTask(_ =&gt; GetItems()), RxApp.TaskpoolScheduler);
  
          LoadItemsCommand
              .ObserveOn(RxApp.MainThreadScheduler)
              .Subscribe(results =&gt;
              {
                  //debugger breaks here in EVERY case and successfully changes List but doesn't necessarily affect the view
  
                  try
                  {
                      List.AddRange(results.Select(e =&gt; new ItemViewModel()
                      {
                          Name = e.Name
                      }));
                  }
                  catch (Exception ex)
                  {
                      //breakpoint does not break here.
                      Debug.WriteLine(ex.Message);
                      throw;
                  }
  
              });
  
          //No exceptions here either
          LoadItemsCommand.ThrownExceptions
              .Select(ex =&gt; new UserError("Error", "Please check your Internet connection"))
              .Subscribe(Observer.Create&lt;UserError&gt;(x =&gt; UserError.Throw(x)));
  
          this.WhenAnyValue(e =&gt; e.SearchText).Subscribe(e =&gt; ResetPage());
      }
  
      private Task&lt;IEnumerable&lt;Item&gt;&gt; GetItems()
      {
          //asynchronously get items
          return ...;
      }
  
      private int ResetPage()
      {
          List.Clear();
          return 0;
      }
  
      [DataMember]
      public ReactiveList&lt;ItemViewModel&gt; List { get; private set; }
  
      private string _searchText;
      [DataMember]
      public string SearchText
      {
          get { return _searchText; }
          set { this.RaiseAndSetIfChanged(ref _searchText, value); }
      }
  
      public ReactiveCommand&lt;IEnumerable&lt;Item&gt;&gt; LoadItems { get; protected set; }
  
      public class ItemViewModel : ReactiveObject
      {
          public string Name { get; set; }
      }
  
      public string UrlPathSegment
      {
          get { return "Page"; }
      }
  
      public IScreen HostScreen { get; protected set; }
  
  
  }
  </code></pre>
  
  <p>My xaml:
      
      </p>
  
  <pre><code>  &lt;StackLayout VerticalOptions="FillAndExpand" Orientation="Vertical"&gt;
      &lt;Entry x:Name="_searchEntry" Placeholder="Search"&gt;&lt;/Entry&gt;
      &lt;ListView x:Name="_myListView" RowHeight="80"&gt;
        &lt;ListView.ItemTemplate&gt;
          &lt;DataTemplate&gt;
            &lt;ViewCell&gt;
              &lt;StackLayout Orientation="Vertical" &gt;
                &lt;Label Text="{Binding Name}"&gt;&lt;/Label&gt;
                &lt;Label Text="{Binding Address}"&gt;&lt;/Label&gt;
                &lt;Label Text="{Binding PhoneNumber}"&gt;&lt;/Label&gt;
              &lt;/StackLayout&gt;
            &lt;/ViewCell&gt;
          &lt;/DataTemplate&gt;
        &lt;/ListView.ItemTemplate&gt;
      &lt;/ListView&gt;
      &lt;Button x:Name="_loadMoreButton" Text="Load More" TextColor="White" BackgroundColor="#77D065"&gt;&lt;/Button&gt;  &lt;/StackLayout&gt;
  &lt;/XamForms:ReactiveContentPage&gt;
  </code></pre>
  
  <p>My view code-behind:</p>
  
  <pre><code>using System;
  using System.Reactive.Concurrency;
  using System.Threading.Tasks;
  
  using ReactiveUI;
  using ReactiveUI.XamForms;
  
  namespace views
  {
      public partial class MyView : ReactiveContentPage&lt;MyViewModel&gt;
      {
          private IDisposable _disconnectHandler;
  
          public NearbyPlacesView()
          {
              InitializeComponent();
  
              this.Bind(this.ViewModel, model =&gt; model.SearchText, view =&gt; view._searchEntry.Text);
              this.OneWayBind(this.ViewModel, model =&gt; model.List, view =&gt; view._myListView.ItemsSource);
              this.BindCommand(this.ViewModel, model =&gt; model.LoadItemsCommand, view =&gt; view._loadMoreButton);
          }
  
          protected override void OnAppearing()
          {
              base.OnAppearing();
  
              //Is this the proper way to do this? seems
              _disconnectHandler = UserError.RegisterHandler(async error =&gt;
              {
                  RxApp.MainThreadScheduler.ScheduleAsync(async (scheduler, token) =&gt;
                  {
                      await DisplayAlert("Error", error.ErrorMessage, "OK");
                  });
  
                  return RecoveryOptionResult.CancelOperation;
              });
  
              //Load the items when the view appears. This doesn't feel right though.
              ViewModel.LoadItemsCommand.Execute(null);
          }
  
          protected override void OnDisappearing()
          {
              base.OnDisappearing();
  
              _disconnectHandler.Dispose();
              _disconnectHandler = null;
          }
      }
  }
  </code></pre>