public InfiniteCollection() { InitializeComponent(); this.WhenAnyValue(x => x.ViewModel) .Where(x => x != null) .Select(x => Unit.Default) .InvokeCommand(this, x => x.ViewModel.InitializeData) .DisposeWith(PageBindings); this.WhenAnyValue(x => x.ViewModel.Items) .Where(x => x != null) .BindTo(this, x => x.Collection.ItemsSource) .DisposeWith(PageBindings); Collection .Events() .SelectionChanged .Subscribe(_ => Collection.SelectedItem = null) .DisposeWith(PageBindings); var loadPressed = Load .Events() .Pressed .Select(x => Unit.Default) .Do(_ => this.Log().Debug($"{nameof(Load.Pressed)}")); var itemThresholdReached = Collection .Events() .RemainingItemsThresholdReached .Select(x => Unit.Default) .Do(_ => this.Log().Debug($"{nameof(Collection.RemainingItemsThresholdReached)}")); itemThresholdReached .Merge(loadPressed) .Throttle(TimeSpan.FromSeconds(10), RxApp.TaskpoolScheduler) .Do(_ => this.Log().Debug("Merged Observable")) .ObserveOn(RxApp.MainThreadScheduler) .InvokeCommand(this, x => x.ViewModel.Load) .DisposeWith(PageBindings); }
public InfiniteScroll() { InitializeComponent(); this.OneWayBind(ViewModel, x => x.IsRefreshing, x => x.ListView.IsRefreshing) .DisposeWith(PageBindings); this.OneWayBind(ViewModel, x => x.IsLoading, x => x.Activity.IsRunning) .DisposeWith(PageBindings); this.OneWayBind(ViewModel, x => x.IsLoading, x => x.Activity.IsVisible) .DisposeWith(PageBindings); this.Bind(ViewModel, x => x.SearchText, x => x.Search.Text) .DisposeWith(PageBindings); this.WhenAnyValue(x => x.ViewModel.Items) .Where(x => x != null) .BindTo(this, x => x.ListView.ItemsSource) .DisposeWith(PageBindings); ListView .Events() .Refreshing .Select(x => ((IList <InventoryItemViewModel>)ListView.ItemsSource).Count) .InvokeCommand(this, x => x.ViewModel.Load) .DisposeWith(PageBindings); ListView .Events() .ItemSelected .Subscribe(item => { ListView.SelectedItem = null; }) .DisposeWith(PageBindings); var itemAppearing = ListView .Events() .ItemAppearing; itemAppearing .Throttle(TimeSpan.FromMilliseconds(300), RxApp.TaskpoolScheduler) .Where(x => (x.Item as InventoryItemViewModel)?.Id == ((IList <InventoryItemViewModel>)ListView.ItemsSource)?.Last()?.Id) .Select(x => x.ItemIndex) .ObserveOn(RxApp.MainThreadScheduler) .InvokeCommand(this, x => x.ViewModel.Load) .DisposeWith(PageBindings); Load .Events() .Pressed .Select(x => ((IList)ListView.ItemsSource).Count) .ObserveOn(RxApp.MainThreadScheduler) .InvokeCommand(this, x => x.ViewModel.Load) .DisposeWith(PageBindings); #region Original /* This is the original example for Item Appearing and Infinite Scroll * Taken from https://github.com/RobGibbens/InfiniteScrolling/blob/master/InfiniteScrolling/MainPage.xaml.cs */ // this.ListView.ItemAppearing += async (sender, e) => // { // var itemViewModel = (InventoryItemViewModel) e.Item; // // this.Log().Debug("stuff about the item"); // if (!ViewModel.IsLoading) // { // // if (itemViewModel.Name == ViewModel.Items.Last().Name) // { // await LoadData(); // } // } // }; // } // // async Task LoadData () // { // await ViewModel.LoadData (_start, _numberOfRecords); // _start = _start + _numberOfRecords; // } #endregion }