|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Collections.ObjectModel; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace ListViewSample |
| 8 | +{ |
| 9 | + public class LoadMoreViewModel |
| 10 | + { |
| 11 | + private readonly int totalItems = 15; |
| 12 | + public ObservableCollection<BookInfo> BookDetails { get; set; } |
| 13 | + public Command<object> LoadMoreItemsCommand { get; set; } |
| 14 | + |
| 15 | + public LoadMoreViewModel() |
| 16 | + { |
| 17 | + BookDetails = new ObservableCollection<BookInfo>(); |
| 18 | + GenerateSource(5); |
| 19 | + LoadMoreItemsCommand = new Command<object>(LoadMoreItems,CanLoadMoreItems); |
| 20 | + } |
| 21 | + |
| 22 | + private bool CanLoadMoreItems(object obj) |
| 23 | + { |
| 24 | + if(BookDetails.Count >= totalItems) |
| 25 | + { |
| 26 | + return false; |
| 27 | + } |
| 28 | + return true; |
| 29 | + } |
| 30 | + |
| 31 | + private async void LoadMoreItems(object obj) |
| 32 | + { |
| 33 | + var listView = obj as Syncfusion.Maui.ListView.SfListView; |
| 34 | + if(listView != null ) |
| 35 | + { |
| 36 | + listView.IsLazyLoading = true; |
| 37 | + await Task.Delay(2500); |
| 38 | + var index = BookDetails.Count; |
| 39 | + var count = index + 2 >= totalItems ? totalItems - index : 2; |
| 40 | + for (int i = index; i < index + count; i++) |
| 41 | + { |
| 42 | + var books = new BookInfo() |
| 43 | + { |
| 44 | + Name = BookNames[i], |
| 45 | + Description = BookDescriptions[i] |
| 46 | + }; |
| 47 | + BookDetails.Add(books); |
| 48 | + } |
| 49 | + listView.IsLazyLoading = false; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private void GenerateSource(int value) |
| 54 | + { |
| 55 | + for (int i = 0; i < value; i++) |
| 56 | + { |
| 57 | + var book = new BookInfo() |
| 58 | + { |
| 59 | + Name = BookNames[i], |
| 60 | + Description = BookDescriptions[i] |
| 61 | + }; |
| 62 | + |
| 63 | + BookDetails.Add(book); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private readonly string[] BookNames = new string[] |
| 68 | + { |
| 69 | + "Object-Oriented Programming in C#", |
| 70 | + "C# Code Contracts", |
| 71 | + "Machine Learning Using C#", |
| 72 | + "Neural Networks Using C#", |
| 73 | + "Visual Studio Code", |
| 74 | + "Android Programming", |
| 75 | + "iOS Succinctly", |
| 76 | + "Visual Studio 2015", |
| 77 | + "Xamarin.Forms", |
| 78 | + "Windows Store Apps", |
| 79 | + "Agile Software Development", |
| 80 | + "Assembly Language", |
| 81 | + "Cryptography in .NET", |
| 82 | + "Entity Framework Code First", |
| 83 | + "Localization for .NET" |
| 84 | + }; |
| 85 | + |
| 86 | + private readonly string[] BookDescriptions = new string[] |
| 87 | + { |
| 88 | + "Object-oriented programming is the de facto programming paradigm.", |
| 89 | + "Code Contracts provide a way to convey code assumptions.", |
| 90 | + "You’ll learn several different approaches to applying machine learning.", |
| 91 | + "Neural networks are an exciting field of software development.", |
| 92 | + "It is a powerful tool for editing code and serves for end-to-end programming.", |
| 93 | + "It is provides a useful overview of the Android application lifecycle.", |
| 94 | + "It is for developers looking to step into frightening world of iPhone.", |
| 95 | + "The new version of the widely-used integrated development environment.", |
| 96 | + "Its creates mappings from its C# classes and controls directly.", |
| 97 | + "Windows Store apps present a radical shift in Windows development.", |
| 98 | + "Learning new development processes can be difficult.", |
| 99 | + "Assembly language is as close to writing machine code.", |
| 100 | + "Cryptography is used throughout software to protect all kinds of information.", |
| 101 | + "Follow author Ricardo Peres as he introduces the newest development mode.", |
| 102 | + "Learn to write applications that support different languages and cultures." |
| 103 | + }; |
| 104 | + } |
| 105 | +} |
0 commit comments