|
1 | | -# xamarin-listview-loadmore |
2 | | -Sample Application which showcases the usage of load more option in Xamarin.Forms ListView |
| 1 | +# Processing Xamarin ListView with LoadMore in MVVM |
| 2 | + |
| 3 | +ListView supports binding the [LoadMoreOption](https://help.syncfusion.com/cr/cref_files/xamarin/Syncfusion.SfListView.XForms~Syncfusion.ListView.XForms.SfListView~LoadMoreOption.html), [LoadMoreCommand](https://help.syncfusion.com/cr/cref_files/xamarin/Syncfusion.SfListView.XForms~Syncfusion.ListView.XForms.SfListView~LoadMoreCommand.html), and [IsBusy](https://help.syncfusion.com/cr/cref_files/xamarin/Syncfusion.SfListView.XForms~Syncfusion.ListView.XForms.SfListView~IsBusy.html) properties from view model to load more number of items at runtime. `LoadMoreOption` enables load more manually or automatically when loading the items at runtime. `LoadMoreCommand` executes to load the items form view model. The `IsBusy` property notifies that the items are populating from view model to show or hide the load more view. |
| 4 | + |
| 5 | +The IsBusy property in view model shows the busy indicator when populating the [ItemsSource](https://help.syncfusion.com/cr/cref_files/xamarin/Syncfusion.SfListView.XForms~Syncfusion.ListView.XForms.SfListView~ItemsSource.html). |
| 6 | + |
| 7 | +``` |
| 8 | +<syncfusion:SfListView x:Name="listView" |
| 9 | + LoadMoreOption="Auto" |
| 10 | + LoadMoreCommand="{Binding LoadMoreItemsCommand}" |
| 11 | + LoadMoreCommandParameter="{Binding Source={x:Reference Name=listView}}" |
| 12 | + IsBusy="{Binding IsBusy}" |
| 13 | + ItemsSource="{Binding BookInfoCollection}"> |
| 14 | +``` |
| 15 | + |
| 16 | +``` |
| 17 | +public class ViewModel:INotifyPropertyChanged |
| 18 | +{ |
| 19 | + private bool isBusy; |
| 20 | + public bool IsBusy |
| 21 | + { |
| 22 | + get { return isBusy; } |
| 23 | + set |
| 24 | + { |
| 25 | + this.isBusy = value; |
| 26 | + RaisePropertyChanged("IsBusy"); |
| 27 | + } |
| 28 | + } |
| 29 | + private int totalItems = 22; |
| 30 | + public Command<object> LoadMoreItemsCommand { get; set; } |
| 31 | +
|
| 32 | + public ViewModel() |
| 33 | + { |
| 34 | + BookInfoCollection = new ObservableCollection<BookInfo>(); |
| 35 | + AddBookInfos(0, 3); |
| 36 | + LoadMoreItemsCommand = new Command<object>(LoadMoreItems); |
| 37 | + } |
| 38 | +
|
| 39 | + private bool CanLoadMoreItems(object obj) |
| 40 | + { |
| 41 | + if (BookInfoCollection.Count >= totalItems) |
| 42 | + return false; |
| 43 | + return true; |
| 44 | + } |
| 45 | +
|
| 46 | + private async void LoadMoreItems(object obj) |
| 47 | + { |
| 48 | + var listview = obj as Syncfusion.ListView.XForms.SfListView; |
| 49 | + try |
| 50 | + { |
| 51 | + IsBusy = true; |
| 52 | +
|
| 53 | + await Task.Delay(1000); |
| 54 | +
|
| 55 | + var index = BookInfoCollection.Count; |
| 56 | + var count = index + 3 >= totalItems ? totalItems - index : 3; |
| 57 | + AddBookInfos(index, 3); |
| 58 | +
|
| 59 | + } |
| 60 | + catch |
| 61 | + { |
| 62 | +
|
| 63 | + } |
| 64 | + finally |
| 65 | + { |
| 66 | + IsBusy = false; |
| 67 | + } |
| 68 | + } |
| 69 | +
|
| 70 | + private void AddBookInfos(int index, int count) |
| 71 | + { |
| 72 | + int bookNameCount = 0; |
| 73 | + int bookDescriptionCount = 0; |
| 74 | + for (int i = index; i < index + count; i++) |
| 75 | + { |
| 76 | + if (bookNameCount == 11) |
| 77 | + bookNameCount = 0; |
| 78 | + if (bookDescriptionCount == 11) |
| 79 | + bookDescriptionCount = 0; |
| 80 | + BookInfoCollection.Add(new BookInfo() { BookName = bookNames[bookNameCount] , BookDescription = bookDescription[bookDescriptionCount] }); |
| 81 | + bookNameCount++; |
| 82 | + bookDescriptionCount++; |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | +You can also refer our UG documentation to know more about [MVVM](https://help.syncfusion.com/xamarin/sflistview/mvvm). |
0 commit comments